Mybatis integration with Spring is actually SM integration in SSM framework.

The idea of integration is actually the core of Mybatis integration of Spring

The SqlSessionFactory object should be placed in the Spring container as a singleton. Spring is a singleton by default. 2. In traditional DAO development, sqlSession objects should be obtained from the Spring container. In Mapper proxy form, Mapper proxy objects should be obtained directly from the Spring container. 4. Database connection and database connection pool transaction management are left to the Spring container.

2. Integrate required JAR package 1, Spring JAR package 2, Mybatis JAR package 3, Spring+ Mybatis integration package. Mysql database driver JAR package Jar package for database connection pool.

Jar package, I hope you had better have, or I will be embarrassed QAQ

Create a Java project as shown below:

Small white Gospel, Mybatis integration spring tutorial sharing

3.2. Importing jar packages The jar packages mentioned above need to be imported, as shown in the following figure:

Small white Gospel, Mybatis integration spring tutorial sharing

3.3. Adding configuration Files 1. MybatisSpring configuration file 2. A) database connection and connection pool b) transaction management (temporarily optional) c)sqlsessionFactory object, configured into the Spring container d) Mapeer proxy object or DAO implementation class configured into the Spring container.

Create a copy of the resource folder config and add it to the configuration file as shown in the following figure

Small white Gospel, Mybatis integration spring tutorial sharing

The sqlmapconfig. XML configuration file is sqlmapconfig. XML, as follows:

3.3.2 rainfall distribution on 10-12. ApplicationContext. XML SqlSessionFactoryBean belongs to mybatis – spring the jars for spring, mybatis is another architecture, need to integrate the jar package.

Add the source code for mybatis-spring-1.2.2.jar to the project, as shown below

Mybatis integrate spring tutorial to share small white Gospel

The result is as shown in the figure below. If the icon changes, the source code is loaded successfully:

SqlSessionFactoryBean = SqlSessionFactoryBean

Small white Gospel, Mybatis integration spring tutorial sharing

Applicationcontext.xml is configured as follows

Since it was the original Dao development, so

. 3.3.3. Configuration db. The properties of JDBC driver = com. Mysql. JDBC. Driverjdbc. Url = JDBC: mysql: / / localhost: 3306 / mybatis? CharacterEncoding = utf-8 JDBC. Username = rootjdbc. Password = root 3.3.4. Configure log4j properties

Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output… log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.a ppender.stdout.layout.ConversionPattern=%5p [%t] – %m%n

3.3.5. Effect: The final effect of the added configuration file is as follows:

Small white Gospel, Mybatis integration spring tutorial sharing so far environment is built OK !!!!

4.Dao development can be implemented in two ways: 1. Original Dao development mode 2

Requirements: 1. Query by user ID 2. Fuzzy query by user name 3. Add user

4.1. To create a pojo

public class User { private int id; private String username; Private String sex; // private Date birthday; // private String address; // address get/set… 4.2. Traditional DAO development method (Method 1) the original DAO development interface + implementation class to complete. The DAO implementation class needs to inherit from the SqlsessionDaoSupport class

4.2.1. Implement mapper. XML Compile user. XML configuration file as follows:

4.2.2. Loading mapper. XML Configure SqlMapConfig as shown in the following figure:

Small white Gospel, Mybatis integration spring tutorial sharing

Public interface UserDao {/** * queryUserById ** @param id * @return/User queryUserById(int id); /* * @param username * @return/List queryUserByUsername(String username); /* * save * * @param user */ void saveUser(user user); } 4.2.4. Write UserDaoImpl implementation interface and inherit SqlSessionDaoSupport to write DAO implementation class, SqlSessionDaoSupport SqlSessionDaoSupport provides the getSqlSession() method to retrieve SqlSession

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User queryUserById(int id) { SqlSession SqlSession = super.getsQLSession (); User User = sqlsession. selectOne(“queryUserById”, id); // do not close sqlSession return user; } @override public List queryUserByUsername(String username) {SqlSession SqlSession SqlSession = super.getSqlSession(); List List = sqlsession. selectList(“queryUserByUsername”, username); // do not close sqlSession return list; } @override public void saveUser(User User) {// Obtain SqlSession SqlSession SqlSession = super.getsQLSession (); Sqlsession. insert(“saveUser”, user); / / don’t have to submit, transaction managed by the spring / / don’t close the sqlSession}} 4.2.4.1 SqlSessionDaoSupport source code The implementation class must inherit SqlSessionDaoSupportSqlSessionDaoSupport provide getSqlSession () method to get the SqlSession

Small white Gospel, Mybatis integration spring tutorial sharing

4.2.5. Configuring dao Configure the DAO implementation class into the Spring container

Create test methods that directly create test Junit use cases. Create it as shown in the following figure.

Mybatis integrate spring tutorial to share the small white Gospel, Mybatis integrate Spring tutorial to share the small white Gospel

Write the test method as follows:

public class UserDaoTest { private ApplicationContext context; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”); } @test public void testYUserById () {userDao userDao = this.context.getBean(userdao.class); User user = userDao.queryUserById(1); System.out.println(user); } @test public void testYUserByUsername () {userDao userDao = this.context.getBean(userdao.class); The List List = userDao. QueryUserByUsername (” zhang “); for (User user : list) { System.out.println(user); }} @test public void testSaveUser() {// Get userDao userDao = this.context.getBean(userDao. User user = new User(); User.setusername (” xiahou Dun pit “); user.setSex(“1″); user.setBirthday(new Date()); User. SetAddress (” three kingdoms “); userDao.saveUser(user); System.out.println(user); }} 5. Dao development in Mapper proxy form (Mode 2) 5.1 Usermapper. XML configuration file, as follows:

Public interface UserMapper {/** * queryUserById(int ID); /* * @param username * @return/List queryUserByUsername(String username); /* * add user * * @param user */ void saveUser(user user); Add MapperFactoryBean configuration to ApplicationContext. XML as part of myBatis – Spring integration package

Public class UserMapperTest {private ApplicationContext context; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”); } @test public void testQueryUserById() {// Get Mapper UserMapper UserMapper = this.context.getBean(usermapper.class); User user = userMapper.queryUserById(1); System.out.println(user); } @test public void testYUserByUsername () {// Obtain Mapper UserMapper UserMapper = this.context.getBean(UserMapper.class); List the List = userMapper. QueryUserByUsername (” zhang “); for (User user : list) { System.out.println(user); }} @test public void testSaveUser() {// Get Mapper UserMapper UserMapper = this.context.getBean(usermapper.class); User user = new User(); User.setusername (” Angela “); user.setSex(“1″); user.setBirthday(new Date()); User. SetAddress (” China “); userMapper.saveUser(user); System.out.println(user); 5.5. Mode 2: Configure Mapper in Scanning package mode (common development)

The ID of each Mapper proxy object is the class name, starting with a lowercase letter

6, the realization of Dao development summary figure small white Gospel, Mybatis integration of Spring tutorial sharing

If you think this article is good, then like to forward it ~ more detailed technical information please pay attention to this nuggets good, later will give you to do a share!