SPRING3 Integrated MYBATIS3

Source: Internet
Author: User
Tags config

Just put Spring3 and MYBATIS3 integrated, record. This article is only for spring and mybatis integration, not too complex applications, configuration is relatively simple, if there is a need, you can add more detailed configuration according to their own needs.

Jar Preparation

Mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.1.21
Commons-dbcp-1.4.jar
Commons-pool-1.6.jar
Commons-logging-1.1.1.jar
Org.springframework.beans-3.1.2.release.jar
Org.springframework.core-3.1.2.release.jar
Org.springframework.context-3.1.2.release.jar
Org.springframework.tx-3.1.2.release.jar

1 Writing Entities

Package Com.ywxm.bean;

public class Type {

	private int id;
	private String name;

	public int getId () {
		return ID;
	}

	public void setId (int id) {
		this.id = ID;
	}

	Public String GetName () {
		return name;
	}

	public void SetName (String name) {
		this.name = name;
	}
}

2 Writing Application.properties

Jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/ywxm
jdbc.username=root
Jdbc.password=root
3 Writing the spring configuration file Applicationcontext.xml

     <!--using annotations to automatically inject beans--<context:component-scan base-package= "Com.ywxm"/> <context:property-placehold Er location= "classpath*:/application.properties"/> <!--data Source configuration-<bean id= "DataSource" class= "Org.apache . Commons.dbcp.BasicDataSource "> <property name=" driverclassname "value=" ${jdbc.driver} "/> <property Name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <property name= "pa ssWOrd "value=" ${jdbc.password} "/> </bean> <!--mybatis configuration-<bean id=" Sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" DataSource "/> <property Name= "Configlocation" value= "Classpath:mybatis.xml"/> <property name= "mapperlocations" value= "classpath: Mapper/*_mapper.xml "/> </bean> <!--transaction Management--<bean id=" TransactionManager "class=" org . springframework.jdbc.datasource.DataSourCetransactionmanager "> <property name=" dataSource "ref=" DataSource "/> </bean> <tx : Annotation-driven transaction-manager= "TransactionManager"/>

4 Write MyBatis configuration file Mybatis.xml (in fact, this configuration can be anything without configuration)

<?xml version= "1.0" encoding= "UTF-8"?> <!  
DOCTYPE configuration Public  "-//mybatis.org//dtd Config 3.0//en" 
"http://mybatis.org/dtd/ Mybatis-3-config.dtd ">

<configuration>

	<typeAliases>
		<typealias type=" Com.ywxm.bean.Type "alias=" Type "/>
	</typeAliases>

</configuration>
5 Writing Mapper.xml files

<?xml version= "1.0" encoding= "UTF-8"?> <!

DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en"
 "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD" >

<mapper namespace= "type" >

	<resultmap type= "Com.ywxm.bean.Type" id= "type" >
		<id column= " ID "property=" id "/>
		<result column=" name "property=" name "/>
	</resultMap>

	<insert id= "Save" parametertype= "type" >
		insert INTO
		type (id,name) VALUES (#{id},#{name})
	</insert>
        
        <!--ibatis configuration id= #id #, Mysqlbatis configuration id=#{id}-->
	<select id= "get" parametertype= "int" resulttype= " Type ">
		select * from type where id=#{id}
	</select>

</mapper>
6 DAO Writing

Package Com.ywxm.dao;

Import org.mybatis.spring.SqlSessionTemplate;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Com.ywxm.bean.Type;

@Component public
class Typedao {
	

	@Autowired
	private sqlsessiontemplate sqlsessiontemplate;

	public void Setsqlsessiontemplate (Sqlsessiontemplate sqlsessiontemplate) {
		this.sqlsessiontemplate = sqlsessiontemplate;
	}
	
	Public Type get (int id) {
		return Sqlsessiontemplate.selectone ("Type.get", id);
	}
	
	

	public void Save (type type) {
		Sqlsessiontemplate.insert (". Save", type);
	}
}

Note that the namespace value (in Mapper.xml) is not used in the insert ("Sava", user) in the image above, so it is important to ensure that "save" is unique in all mapper files. I just made this mistake, the result is the database operation is not successful, but no error. Wasted a lot of time. It makes me feel mybatis the test is a bit difficult. If you use the namespace value (example: SelectOne ("Type.get", id)) Then there is the same "get" in the mapper file, so it doesn't matter.
7 Service Authoring

Package com.ywxm.service;

Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import org.springframework.transaction.annotation.Transactional;

Import Com.ywxm.bean.Type;
Import Com.ywxm.dao.TypeDao;

@Component
@Transactional Public
class Typemanager {

	@Autowired
	private Typedao type;

	public void SetType (Typedao type) {
		this.type = type;
	}

	public void Save (type type) {
		this.type.save (type);

	}
	public void get (int id) {
	   Type t=type.get (ID);
	   
	}

}


In the above configuration DAO inherits Sqlsessiondaosupport, if you do not want to use inheritance, we can make a slight change in the spring configuration file Applicationcontext.xml add

    <!--  configuration Sqlsessiontemplate--
    <bean id= "sqlsessiontemplate" class= " Org.mybatis.spring.SqlSessionTemplate ">
	<constructor-arg index=" 0 "ref=" sqlsessionfactory "/>
    

Modify the DAO as follows

Package Com.ywxm.dao;

Import org.mybatis.spring.SqlSessionTemplate;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Com.ywxm.bean.Type;

@Component public
class Typedao {
	

	@Autowired
	private sqlsessiontemplate sqlsessiontemplate;

	public void Setsqlsessiontemplate (Sqlsessiontemplate sqlsessiontemplate) {
		this.sqlsessiontemplate = sqlsessiontemplate;
	}
	
	Public Type get (int id) {
		return Sqlsessiontemplate.selectone ("Type.get", id);
	}
	
	

	public void Save (type type) {
		Sqlsessiontemplate.insert ("Type.save", type);
	}
}

Then why do you do this, by looking at the Sqlsessiondaosupport class to find that there is a passage

"This class needs a sqlsessiontemplate or a sqlsessionfactory. If Both is set the sqlsessionfactory would be ignored. "

That is to say, sqlsessiontemplate or sqlsessionfactory. Look at its source. Sqlsessionfactory is passed as a parameter to sqlsessiontemplate. Either Sqlsessiontemplate or sqlsessionfactory. Finally, Sqlsessiontemplate instances are obtained. by Sqlsessio Ntemplate the database operation. So give it a sqlsessiontemplate instance. Using Sqlsessiontemplate without using Sqlsessiondaosupport can also succeed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.