spring框架整合mybatis
讓我們一起來感受框架的強大,和spring配置地獄的瘋狂吧
專案檔案目錄

組態檔
spring的核心配置 /spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- DataSource:使用Spring的資料源替換Mybatis的配置-->
<!-- 這里使用spring提供的jdbc-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 系結mybatis組態檔-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 系結mybatis組態檔中的Mapper介面的xml檔案-->
<property name="mapperLocations" value="classpath:com/huang/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用構造器注入sqlSessionFactory,因為它沒有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
mybatis核心配置 /mybatis-config.xml
該檔案中通常都只留下設定和別名,其余可以在spring組態檔配置
<?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>
<!-- <properties resource="db.properties"/>-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<typeAliases>
<package name="com.huang.pojo"/>
</typeAliases>
</configuration>
最終讀取的組態檔 /applicationContext.xml 將控制層的業務放在此檔案,同時整合/spring-dao.xml檔案中資料源和sqlSession等的配置,最后CPX中讀取該檔案即可,實作了組態檔的分工
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.huang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
提示:在實作了以上三個組態檔的分層,spring的核心配置 /spring-dao.xml基本可以寫死,可以作為默認檔案保存為模板,以后直接使用即可,簡化了我們的操作
pojo類
lombok配置在上篇博客已經有說明
package com.huang.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
Mapper介面和介面xml和實作類
public interface UserMapper {
List<User> getUserList(int id);
}
<?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.huang.mapper.UserMapper">
<select id="getUserList" resultType="user">
select * from mybatis.user where id = #{id};
</select>
</mapper>
關鍵是實作mapper介面類的代碼(注意理解各個檔案之間的邏輯)
package com.huang.mapper;
import com.huang.pojo.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UserMapperImpl implements UserMapper{
private SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getUserList(int id) {
return sqlSession.getMapper(UserMapper.class).getUserList(id);
}
}
我們不必再通過new來得到sqlSession了,而是使用spring的set注入來獲得sqlSession物件,
業務邏輯的介面都在application.xml中使用bean來配置 此處一定要理解各個檔案之間的邏輯與聯系,
<bean id="userMapper" class="com.huang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
測驗類
public class UserMapperTest {
@Test
public void test1() throws IOException {
//原來沒整合前的代碼
// String resources = "mybatis-config.xml";
// InputStream inputStream = Resources.getResourceAsStream(resources);
// SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// SqlSession sqlSession = sqlSessionFactory.openSession(true);
// UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// List<User> userList = mapper.getUserList();
// for (User user : userList) {
// System.out.println(user);
// }
// sqlSession.close();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.getUserList(1)) {
System.out.println(user);
}
}
}
整合易錯點:
org.springframework.core.ResolvableType org.springframework.beans.factory.co
spring-framework 核心包版本必須保持一致

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255197.html
標籤:java
上一篇:Java 編程基礎語法篇
