1,導包
Spring,MyBatis,Spring和MyBatis連接配置
2,添加Spring配置文件
可以在spring的配置文件中添加SqlSessionFactoryBean來將MyBatis代替。其中需要連接池和導入映射文件,注入SqlSessionFactory。
spring_MyBatis.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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 id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="Yuquan980730"></property>
</bean>
<!-- SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入連接池 -->
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/it/entity/*.xml"></property>
</bean>
<!-- 配置MapperScannerConfigurer,這個配置中會自動生成映射器的實現方法,並調用,注入到Spring容器中 -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入映射器所在的包名 -->
<property name="basePackage" value="com.it.dao"></property>
<property name="annotationClass" value="com.it.dao.MyBatisRepository"></property>
</bean>
</beans>
3,實體類
Person.java
4,映射文件PersonMapper.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">
<!-- 寫Sql語句 -->
<mapper namespace="com.it.dao.PersonDao">
<!-- id:要求唯一 parameterType:參數類型,填寫實體類的完整名字 -->
<insert id="save" parameterType="com.it.entity.Person">
INSERT INTO PERSON VALUES(seq_newsId.nextval,#{pname},#{pwd})
</insert>
<select id="queryAll" resultType="com.it.entity.Person">
SELECT * FROM PERSON
</select>
<select id="queryOne" parameterType="int" resultType="com.it.entity.Person">
SELECT * FROM PERSON WHERE pid = #{rtttt}
</select>
<update id="update" parameterType="com.it.entity.Person">
UPDATE PERSON SET pname=#{pname},pwd=#{pwd} WHERE pid = #{pid}
</update>
<delete id="delete" parameterType="int">
DELETE FROM PERSON WHERE pid = #{asdasd}
</delete>
<!-- 利用Map類型的參數進行獲取參數-->
<select id="MapReturnQueryOne" parameterType="int" resultType="map">
SELECT * FROM PERSON WHERE pid=#{pid}
</select>
<!-- 利用修改別名來來解決實體類和數據庫表名不同的問題 -->
<select id="queryEmp" parameterType="int" resultType="com.it.entity.Emp">
SELECT PID AS empNo,PNAME AS empName,PWD FROM PERSON WHERE PID=#{ADAD}
</select>
<resultMap type="com.it.entity.Emp" id="PersonResultMap">
<result property="empNo" column="pid"/>
<result property="empName" column="pname"/>
</resultMap>
<!-- 通過ResultMap方法對屬性名不一致的問題進行明確匹配 -->
<select id="queryEmpAll" resultMap="PersonResultMap" parameterType="int">
SELECT * FROM PERSON WHERE pid=#{ID}
</select>
</mapper>
5,Mapper映射器
PersonDao.java
package com.it.dao;
//Mapping映射器:符合MyBatis的映射文件的接口,
//會生成符合該接口的對象,之後調用方法就可以執行映射文件中的內容
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.it.entity.Person;
@Repository("personDAO")
@MyBatisRepository
public interface PersonDao {
public void save(Person e);
public List<Person> queryAll();
public Person queryOne(int id);
public void delete(int id);
public void update(Person a);
//Map接收參數 優缺點 優點:只需要接收Map類型的鍵值對,不需要實體類。缺點:在調用屬性值的時候需要調用get方法。
public Map MapReturnQueryOne(int id);
//對不同屬性名的別名處理
public Person queryEmp(int id);
//對不同屬性名的ResultMap方法處理
/*public resultMap queryEmpAll(int id); */
}
6,配置MapperScannerConfigurer Bean
不集成的情況下 Mapping映射器寫好後,調用SqlSession.getMapper(PersonDao.class)來自動生成符合映射器接口的對象
集成的情況下需要配置
MapperScannerConfigurer:
bean會掃描指定包和子包下所有的Mapper映射器,並自動調用getMapper方法,來調用映射器的實現方法。並將這些對象添加到Spring容器中。
<!-- 配置MapperScannerConfigurer,這個配置中會自動生成映射器的實現方法,並調用,注入到Spring容器中 -->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入映射器所在的包名 -->
<property name="basePackage" value="com.it.dao"></property>
<property name="annotationClass" value="com.it.dao.MyBatisRepository"></property>
</bean>
7,Spring映射器測試
String config="spring_MyBatis.xml";
//啓動容器
ApplicationContext ac=new ClassPathXmlApplicationContext(config);
//將映射器名稱大寫改小寫,稱爲getBean的id
PersonDao dao=ac.getBean("personDao",PersonDao.class);
//其中小寫映射器名爲id,只需要在映射器中加入@Repository("personDAO")也可以將id自由改變,此處的註解掃描 在配置MapperScannerConfigurer過程中自動掃描
//id默認爲接口的首字母小寫,也可以添加@Repository進行自由修改
List<Person> person=dao.queryAll();
for(Person a:person) {
System.out.println(a);
}
在MapperScannerConfigurer的bean中會掃描指定包和子包下的所有Mapper映射器,爲了有選擇性的掃描需要自定義一個註釋。
適用情況:在開發中,有些映射器不想通過MyBatis進行開發,需要通過註解註釋想要被掃描的映射器。
1, 新建一個空註解接口
package com.it.dao;
public @interface MyBatisRepository {
}
2,將註釋寫在想要掃描到的接口之上
3 ,配置MapperScannerConfigurer
添加方法
<property name="annotationClass" value="com.it.dao.MyBatisRepository"></property>
將想要通過Configurer掃描的掃描器上的註釋,通過方法進行注入。
如果去掉註釋,表示不讓Configurer對此註釋下的映射器進行掃描,就無法找到正確的映射器位置。