javaEE Mybatis,Mybatis與Spring整合之動態代理方式(推薦),自動建立Dao層實現類
阿新 • • 發佈:2018-12-11
src/applicationContext.xml(Spring核心配置檔案):
<?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"> <context:property-placeholder location="classpath:db.properties"/> <!-- DBCP資料庫連線池 --> <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的工廠 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- Mybatis核心配置檔案的位置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!-- Mapper動態代理開發。 註冊方式 一(不推薦):所有的Dao層介面都需要在Spring中註冊,比較繁瑣;可以使用掃描基本包的方式一次性註冊多個。 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> <!-- 注入Dao層介面(遵循四個原則的介面)。 會自動建立對應Dao層實現類(動態代理)。 --> <property name="mapperInterface" value="com.xxx.mybatis.mapper.UserMapper"/> </bean> <!-- Mapper動態代理開發。 註冊方式 二(推薦):掃描基本包下的所有Dao層介面;不需要在Spring中一個一個地註冊 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 會根據型別自動注入Mybatis工廠 --> <!-- 基本包(也會遞迴掃描其下的子包) --> <property name="basePackage" value="com.xxx.mybatis.mapper"/> </bean> </beans>
src/sqlMapConfig.xml(Mybatis核心配置檔案):
<?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.xxx.mybatis.pojo" /> </typeAliases> <!-- 載入外部實體類的Sql對映檔案 --> <mappers> <package name="com.xxx.mybatis.mapper"/> </mappers> </configuration>
UserMapper.java(遵循四個規則的Dao層介面):
package com.xxx.mybatis.mapper; import com.xxx.mybatis.pojo.User; //遵循四個原則的Dao層介面 public interface UserMapper { //遵循四個原則: //UserMapper.xml中配置的名稱空間要與該介面的全類名保持一致(com.xxx.mybatis.mapper.UserMapper) //介面中的方法名 == UserMapper.xml中配置的sql語句的id名 //返回值型別 與 UserMapper.xml檔案中配置的返回值型別(resultType)要一致 //方法的輸入引數型別 與UserMapper.xml中配置的輸入引數的型別(parameterType)要一致 public User findUserById(Integer id); }
UserMapper.xml(Sql配置檔案):
<?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">
<!-- 配置原生Sql語句 -->
<mapper namespace="com.xxx.mybatis.mapper.UserMapper"> <!-- 名稱空間指定為對應Dao層介面的全類名 -->
<!-- 根據ID查詢使用者 -->
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id = #{v}
</select>
</mapper>
Test.java(測試類):
package com.xxx.mybatis.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xxx.mybatis.mapper.UserMapper;
import com.xxx.mybatis.pojo.User;
public class Test {
@Test
public void testMapper() throws Exception {
//建立Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = ac.getBean(UserMapper.class); //動態代理,自動建立Dao層實現類。
//也可以通過Spring容器中配置的bean的id獲取Dao層實現類。 (如果用掃描的方式註冊的Dao層介面,那麼就沒有id,不能用這種方式獲取實現類)
//UserMapper mapper = (UserMapper) ac.getBean("userMapper");
User user = mapper.findUserById(10);
System.out.println(user);
}
}
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123