Spring整合Mybatis框架小demo

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

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

Spring整合Mybatis框架小demo

 

 mybatis-config.xml配置文件:

Spring整合Mybatis框架小demo

 

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <!--配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>

    </bean>
    <!--配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--获取到你的数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--使用下面这种方式获取sql文件  -->
        <property name="mapperLocations">
            <list>
                <value>classpath:cn/smbms/dao/**/*.xml</value>
            </list>
        </property> 
    </bean>
    
    <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
       <!--操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法-->
       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    
    <!--配置dao  -->
    <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl">
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>
    
</beans>

public interface UserMapper {

  

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

}

 

UserMapperImpl.java

package cn.smbms.dao.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.smbms.pojo.User;
import cn.smbms.utils.MyBatisUtil;

public class UserMapperImpl  implements UserMapper{ 

    private SqlSessionTemplate sqlSession;
    
    public SqlSessionTemplate getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    //根据用户姓名和用户角色id查看用户列表信息
    public List<User> getUserList(User user) {
       
        //使用SqlSessionTemplate
        return sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList", user);
                

    }



}

UserMapper.xml

<?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="cn.smbms.dao.user.UserMapper">
<!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
    <resultMap type="User" id="userList">
        <result property="id" column="id"/>
        <result property="userCode" column="userCode"/>
        <result property="userName" column="userName"/>
        <result property="phone" column="phone"/>
        <result property="birthday" column="birthday"/>
        <result property="gender" column="gender"/>
        <result property="userRole" column="userRole"/>
        <result property="userRoleName" column="roleName"/>
    </resultMap>
    
    
    <!-- 查询用户列表(参数:对象入参) -->
    <select id="getUserList" resultMap="userList" parameterType="User">
        select u.*,r.roleName from smbms_user u,smbms_role r
            where u.userName like CONCAT (‘%‘,#{userName},‘%‘) 
                    and u.userRole = #{userRole} and u.userRole = r.id
    </select>
</mapper>

编写测试方法:

@Test
    public void testGetUserList(){
        
        User userCondition = new User();
        userCondition.setUserName("赵");
        userCondition.setUserRole(3);        
        //UserMapper userMapper=new UserMapperImpl();
        ApplicationContext ctx=new 
                ClassPathXmlApplicationContext("applicationContext.xml");
        
        UserMapper userMapper=(UserMapper) ctx.getBean("userMapper");    
        
        
        List<User> userList = userMapper.getUserList(userCondition);
    
        for(User user: userList){
            logger.debug("testGetUserList userCode: " + user.getUserCode() + 
                        " and userName: " + user.getUserName() + 
                        " and userRole: " + user.getUserRole() + 
                        " and userRoleName: " + user.getUserRoleName() +
                        " and age: " + user.getAge() +
                        " and address: " + user.getAddress());
        }
    }
  运行结果:
 
其实上面的UserMapperImpl.java类中的一部分代码还是能够进行优化的。

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <!--配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>

    </bean>
    <!--配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--获取到你的数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--使用下面这种方式获取sql文件  -->
        <property name="mapperLocations">
            <list>
                <value>classpath:cn/smbms/dao/**/*.xml</value>
            </list>
        </property> 
    </bean>
    
    <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作    使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了-->
<!--     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
       操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法
       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean> -->
    
    <!--配置dao  -->
    <bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl">
    <!-- 使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了 -->
        <!-- <property name="sqlSession" ref="sqlSessionTemplate"></property> -->
        <!--但是这时候工程的获取是有必要的了  注意:name="SqlSessionFactory"  首字母大写还是小写这都是可以的  -->
         <property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    
</beans>

运行结果:

Spring整合Mybatis框架小demo

 

 

 

相关推荐