Spring整合Mybatis框架小demo

Posted dongyaotou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合Mybatis框架小demo相关的知识,希望对你有一定的参考价值。

spring框架整合mybatis框架,关键的注释已经写道代码中了

这个小项目的整体结构图:

技术图片

 

 mybatis-config.xml配置文件:

技术图片

 

 spring框架的核心配置文件:applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
14     http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
16 
17     <!--配置数据源 -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
20         <property name="url"
21             value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
22         <property name="username" value="root"></property>
23         <property name="password" value="root"></property>
24 
25     </bean>
26     <!--配置SqlSessionFactoryBean -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <!--获取到你的数据源 -->
29         <property name="dataSource" ref="dataSource"></property>
30         <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
31         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
32         <!--使用下面这种方式获取sql文件  -->
33         <property name="mapperLocations">
34             <list>
35                 <value>classpath:cn/smbms/dao/**/*.xml</value>
36             </list>
37         </property> 
38     </bean>
39     
40     <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作 -->
41     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
42        <!--操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法-->
43        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
44     </bean>
45     
46     <!--配置dao  -->
47     <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl">
48         <property name="sqlSession" ref="sqlSessionTemplate"></property>
49     </bean>
50     
51 </beans>

public interface UserMapper {

  

  /**
  * 查询用户列表(参数:对象入参)
  * @return
  */
  public List<User> getUserList(User user);

}

 

UserMapperImpl.java

 1 package cn.smbms.dao.user;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.mybatis.spring.SqlSessionTemplate;
 9 import org.mybatis.spring.support.SqlSessionDaoSupport;
10 
11 import cn.smbms.pojo.User;
12 import cn.smbms.utils.MyBatisUtil;
13 
14 public class UserMapperImpl  implements UserMapper{ 
15 
16     private SqlSessionTemplate sqlSession;
17     
18     public SqlSessionTemplate getSqlSession() {
19         return sqlSession;
20     }
21 
22     public void setSqlSession(SqlSessionTemplate sqlSession) {
23         this.sqlSession = sqlSession;
24     }
25     //根据用户姓名和用户角色id查看用户列表信息
26     public List<User> getUserList(User user) {
27        
31         //使用SqlSessionTemplate
32         return sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList", user);
33                 
34 
35     }
36 
37 
38 
39 }

UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <mapper namespace="cn.smbms.dao.user.UserMapper">
 7 <!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
 8     <resultMap type="User" id="userList">
 9         <result property="id" column="id"/>
10         <result property="userCode" column="userCode"/>
11         <result property="userName" column="userName"/>
12         <result property="phone" column="phone"/>
13         <result property="birthday" column="birthday"/>
14         <result property="gender" column="gender"/>
15         <result property="userRole" column="userRole"/>
16         <result property="userRoleName" column="roleName"/>
17     </resultMap>
18     
19     
20     <!-- 查询用户列表(参数:对象入参) -->
21     <select id="getUserList" resultMap="userList" parameterType="User">
22         select u.*,r.roleName from smbms_user u,smbms_role r
23             where u.userName like CONCAT (‘%‘,#{userName},‘%‘) 
24                     and u.userRole = #{userRole} and u.userRole = r.id
25     </select>
26 </mapper>

编写测试方法:

 1     @Test
 2     public void testGetUserList(){
 3         
 4         User userCondition = new User();
 5         userCondition.setUserName("赵");
 6         userCondition.setUserRole(3);        
 7         //UserMapper userMapper=new UserMapperImpl();
 8         ApplicationContext ctx=new 
 9                 ClassPathXmlApplicationContext("applicationContext.xml");
10         
11         UserMapper userMapper=(UserMapper) ctx.getBean("userMapper");    
12         
13         
14         List<User> userList = userMapper.getUserList(userCondition);
15     
16         for(User user: userList){
17             logger.debug("testGetUserList userCode: " + user.getUserCode() + 
18                         " and userName: " + user.getUserName() + 
19                         " and userRole: " + user.getUserRole() + 
20                         " and userRoleName: " + user.getUserRoleName() +
21                         " and age: " + user.getAge() +
22                         " and address: " + user.getAddress());
23         }
24     }
25

运行结果:
技术图片

 

其实上面的UserMapperImpl.java类中的一部分代码还是能够进行优化的。
技术图片

 

 applicationContext.xml核心配置文件也需要进行相应的修改:

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
14     http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
16 
17     <!--配置数据源 -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
20         <property name="url"
21             value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
22         <property name="username" value="root"></property>
23         <property name="password" value="root"></property>
24 
25     </bean>
26     <!--配置SqlSessionFactoryBean -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <!--获取到你的数据源 -->
29         <property name="dataSource" ref="dataSource"></property>
30         <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
31         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
32         <!--使用下面这种方式获取sql文件  -->
33         <property name="mapperLocations">
34             <list>
35                 <value>classpath:cn/smbms/dao/**/*.xml</value>
36             </list>
37         </property> 
38     </bean>
39     
40     <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作    使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了-->
41 <!--     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
42        操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法
43        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
44     </bean> -->
45     
46     <!--配置dao  -->
47     <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl">
48     <!-- 使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了 -->
49         <!-- <property name="sqlSession" ref="sqlSessionTemplate"></property> -->
50         <!--但是这时候工程的获取是有必要的了  注意:name="SqlSessionFactory"  首字母大写还是小写这都是可以的  -->
51          <property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
52     </bean>
53     
54 </beans>

运行结果:

技术图片

 

 

 


以上是关于Spring整合Mybatis框架小demo的主要内容,如果未能解决你的问题,请参考以下文章

Spring整合Mybatis框架-为业务层添加声明式事务控制 要么都成功要么都失败

SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)

(Spring+SpringMVC+MyBatis)SSM三大框架整合

[技术篇(java)] 教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis

Spring+Mybatis+Druid 整合Demo

SSM 三大框架系列:Spring 5 + Spring MVC 5 + MyBatis 3.5 整合(附源码)