Spring整合Mybatis(一)
Spring中的sqlSessionTemplate就是普通的SqlSession
与单独的Mybatis的区别:
- 在dao层中多了一个impl实现类
- 将Mybatis的工具类写成了xml(spring中集成了SqlSessionFactory工厂类,我们只需要在xml中配置完注入就行)
- 调用方法的时候只需要从Bean中获取对应接口就行
代码如下:
package com.psy.dao;
import com.psy.pojo.User;
public interface UserDao {
/**
* 查询用户id
*
* @param id id
* @return {@link User}
*/
User queryUserById(Integer 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.psy.dao.UserDao">
<select id="queryUserById" resultType="com.psy.pojo.User">
select * from mybatisStudy.users where id = #{id}
</select>
</mapper>
package com.psy.dao;
import com.psy.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
public class UserDaoImpl implements UserDao {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public User queryUserById(Integer id) {
UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
return userDao.queryUserById(id);
}
}
package com.psy.pojo;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private String id;
private String username;
private String password;
}
import com.psy.dao.UserDao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test() {
ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("spring-dao.xml");
UserDao userDao = cp.getBean("userDao", UserDao.class);
System.out.println(userDao.queryUserById(1));
}
}
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.psy.pojo"/>
</typeAliases>
</configuration>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource.DriverManagerDataSource
-->
<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/mybatisStudy?useSSL=false&;useUnicode=true&amp;characterEncoding=UTF-8&;useJDBCCompliantTimezoneShift=true&;useLegacyDatetimeCode=false&;serverTimezone=Asia/Shanghai&;allowMultiQueries=true"/>
<property name="username" value="root"/>
<property name="password" value="123123123"/>
</bean>
<!--sqlSessionFactory:能够创建SqlSessionFactory -->
<bean name="sqSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/psy/dao/*.xml"/>
</bean>
<!--sqlSessionTemplate:就是我们使用哪个的sqlSession-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入,因为他没有set方法-->
<constructor-arg index="0" ref="sqSessionFactory"/>
</bean>
</beans>
<?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.xsd">
<!--相当于学习Mybatis时候加的那个工具类,这里只是吧工具类变成了xml的形式-->
<import resource="spring-dao.xml"/>
<!--扫描dao接口-->
<bean id="userDao" class="com.psy.dao.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>