首页 > 代码库 > Mybatis中Mapper代理形式开发与spring整合

Mybatis中Mapper代理形式开发与spring整合

1.导入jar包

技术分享

2.分包

技术分享

  • cogfig:存放配置文件
  • mapper:存放映射与接口
  • pojo:存放实体类
  • test:测试代码

3.编写配置文件

SqlMapConfig.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>
        <package name="com.pojo"/>
    </typeAliases>

</configuration>

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis01?characterEncoding=utf-8
jdbc.username=root
jdbc.password=toor

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    
</beans>

4.编写实体类、接口、映射

User.java

public class User implements Serializable {
   
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址

    set/get..............................

}

UserMapper.java

public interface UserMapper {
    
    /**
     * 通过ID查User
     * @return
     */
    public User findUserById(Integer id);
    
}

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="com.mapper.UserMapper">
    
    <!-- 通过ID查User -->
    <select id="findUserById" parameterType="Int" resultType="User">
        select * from user where id = #{id}
    </select>

</mapper>

5.编写applicationContext.xml

配置数据库连接池

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:com/config/db.properties"/>
    
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

管理mybatis工厂

    <!-- 管理mybatis工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:com/config/SqlMapConfig.xml"/>
    </bean>

配置mapper代理对象

    <!--  Mapper代理的方式配置方式一,配置mapper代理对象 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mapper.UserMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

6.测试

public class Demo1 {

    @Test
    public void m01(){
        //加载配置文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/config/applicationContext.xml");
        //获取mapper
        UserMapper mapper = (UserMapper) context.getBean("userMapper");
        //执行SQL
        User user = mapper.findUserById(10);
        System.out.println(user);
        
    }
    
}

技术分享

Mapper代理的配置方式二(推荐推荐推荐

  第一种配置方式过于繁琐,有多少个mapper就需要配置多少次

    <!-- Mapper动态代理配置   扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="com.mapper"/>
    </bean>

修改获取bean的方式

UserMapper mapper = (UserMapper) context.getBean(UserMapper.class);

技术分享

 技术分享

Mybatis中Mapper代理形式开发与spring整合