1. 程式人生 > >Mybatis中Mapper代理形式開發與spring整合

Mybatis中Mapper代理形式開發與spring整合

can sna 修改 jar xid oca pac user cal

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整合