美文网首页
Spring 与 Mybatis 整合

Spring 与 Mybatis 整合

作者: tingshuo123 | 来源:发表于2018-07-20 21:55 被阅读2次

    导入整合需要的 jar 架包

    Spring 与 Mybatis 整合除各自的基础jar包之外,再导入一个mybatis-spring.jar就可以了,所以还是挺简单的。


    整合需要的完整基础jar包

    表结构

    表:t_user2

    创建实体Bean

    package com.project.bean;
    
    public class UserBean {
    
        private int id;
        private String name;
        private String pwd;
    
        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;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        @Override
        public String toString() {
            return "UserBean [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
        }
    
    }
    

    持久层接口

    package com.project.dao;
    
    import com.project.bean.UserBean;
    
    public interface IUserDao {
        
        public UserBean findById(int id);
        
        public void add(UserBean user);
    }
    
    

    创建 mapper 映射文件

    在 src/com/project/mapper 目录下创建 UserMapper 配置文件

    <?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">
    
    <!-- namespace 的值与接口名一致,id与接口方法一致 -->
    <mapper namespace="com.project.dao.IUserDao">
        <select id="findById" parameterType="int" resultType="UserBean">
            select u_id as id,u_name as name,u_pwd as pwd 
            from t_user2 where u_id=#{id}
        </select>
        
        <insert id="Add" parameterType="UserBean">
            insert into t_user2(u_name,u_pwd) values(#{name},#{pwd});
        </insert>
    </mapper>
    

    创建业务层接口

    package com.project.service;
    
    import com.project.bean.UserBean;
    
    public interface IUserService {
        
        public UserBean getUser(int id);
        
        public void addUser(UserBean user);
    }
    
    

    创建业务层实现类

    package com.project.service.impl;
    
    import com.project.bean.UserBean;
    import com.project.dao.IUserDao;
    import com.project.service.IUserService;
    
    public class UserServiceImpl implements IUserService {
    
        private IUserDao dao = null;
        
        public IUserDao getDao() {
            return dao;
        }
    
        public void setDao(IUserDao dao) {
            this.dao = dao;
        }
    
        @Override
        public UserBean getUser(int id) {
            
            return dao.findById(id);
        }
    
        @Override
        public void addUser(UserBean user) {
            
            dao.add(user);
        }
    
    }
    
    

    编写 spring 的配置文件

    在 src/config 目录下 创建 srpingConfig.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:aop="http://www.springframework.org/schema/aop"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:tx="http://www.springframework.org/schema/tx" 
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd"
            > 
    
        <!-- ============== 基本Bean ============== -->
        <bean id="UserServiceImpl" class="com.project.service.impl.UserServiceImpl">
            <property name="dao" ref="IUserDao"></property>
        </bean>
        
        <!-- ============== 整合Mybatis ============== -->
        <!-- 配置连接数据库环境 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="url" value="jdbc:mysql://localhost:3306/mybaits"></property>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="username" value="root"></property>
            <property name="password" value=""></property>
        </bean>
        
        <!-- 配置 mybatis 的 SqlSessionFactory 工厂 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 指定环境 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 指定要加载的配置文件 -->
            <property name="configLocation" value="classpath:config/mybatisConfig.xml"></property>
            <!-- 加载所有mapper文件,只能加载一次,在这里加载了,mybatis 配置文件中就不能加载了 -->
            <!-- <property name="mapperLocations" value="classpath:com/project/mapper/*.xml"></property> -->
        </bean> 
        
        <!-- 创建数据映射器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- basePackage 是固定写法,为dao包下所有接口创建动态代理类 -->
            <property name="basePackage" value="com.project.dao"></property>
        </bean>
        
        <!-- 创建事务管理器:针对jdbc或mybatis -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 指定环境, name的dataSource是固定写法,ref是前面创建的环境id -->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    </beans>
    
    

    创建 mybatis 配置文件

    在 src/config 目录下 创建 mybatisConfig.xml 文件

    <?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.project.bean.UserBean" alias="UserBean"/>
        </typeAliases>
        
        <!-- 加载映射文件,也可在 spring 配置文件中配置 -->
        <mappers>
            <mapper resource="com/project/mapper/UserMapper.xml"/>
        </mappers>
    </configuration>
    

    创建测试类

    package com.project.test;
    
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.project.bean.UserBean;
    import com.project.dao.IUserDao;
    
    public class TestMain {
    
        public static void main(String[] args) {
            ApplicationContext contxt = new ClassPathXmlApplicationContext("config/springConfig.xml");
    
            IUserDao dao = (IUserDao) contxt.getBean("IUserDao");
    
            UserBean user = dao.findById(1);
    
            System.out.println(user);
        }
    }
    
    
    

    测试结果:


    测试结果

    小结:Spring 与 MyBatis 整合的核心是spring配置文件,原先在MyBatis的内容,大部分转移到了spring配置文件中,mybatis的对象也都全交由了spring管理。

    相关文章

      网友评论

          本文标题:Spring 与 Mybatis 整合

          本文链接:https://www.haomeiwen.com/subject/nyxcmftx.html