Spring整合Mybatis方式(使用SqlSessionTemplate)
环境准备请看:https://www.cnblogs.com/SweetheartAndPeaches/p/15468527.html
数据库准备
根据数据库建立entity包放一个VoteUser类
VoteUser.java
package entity;
import java.io.Serializable;
import java.util.List;
public class VoteUser implements Serializable{
private String userName;
private String userPwd;
private int userRank;
private String userVote;
private int userPower;
private String applyVote;
//一对多会存放一个关于问卷调查的List
private List<Vote> vote;
public List<Vote> getVote() {
return vote;
}
public void setVote(List<Vote> vote) {
this.vote = vote;
}
public VoteUser(String userName, String userPwd, int userRank, String userVote, int userPower, String applyVote) {
super();
this.userName = userName;
this.userPwd = userPwd;
this.userRank = userRank;
this.userVote = userVote;
this.userPower = userPower;
this.applyVote = applyVote;
}
public VoteUser() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public int getUserRank() {
return userRank;
}
public void setUserRank(int userRank) {
this.userRank = userRank;
}
public String getUserVote() {
return userVote;
}
public void setUserVote(String userVote) {
this.userVote = userVote;
}
public int getUserPower() {
return userPower;
}
public void setUserPower(int userPower) {
this.userPower = userPower;
}
public String getApplyVote() {
return applyVote;
}
public void setApplyVote(String applyVote) {
this.applyVote = applyVote;
}
public VoteUser(String userName, String userPwd, int userRank) {
super();
this.userName = userName;
this.userPwd = userPwd;
this.userRank = userRank;
}
@Override
public String toString() {
return "VoteUser [userName=" + userName + ", userPwd=" + userPwd + ", userRank=" + userRank + ", userVote="
+ userVote + ", userPower=" + userPower + ", applyVote=" + applyVote + ", vote=" + vote + "]";
}
}
两个xml配置
ApplicationContent.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 整合mybatis的envirement标签 -->
<!-- 配置数据源 -->
<!--这里的org.apache.commons.dbcp.BasicDataSource类是jar包里面的类 ,destroy-method定义数据库关闭的方式-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 参数就是自己的数据库参数 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/survey?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="747452" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置sqlSessionFactory,SqlSessionFactoryBean是用来产生sqlSessionFactory的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的全局配置文件,放在classpath下的mybatis文件夹中 -->
<property name="configLocation" value="classpath:Mybatis-Config.xml" />
<!-- 加载数据源,使用上面配置好的数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 注册mepper.xml -->
<property name="mapperLocations" value="classpath:dao/*.xml" />
</bean>
<!-- 开启扫描:方便我们注入 -->
<context:component-scan base-package="*"></context:component-scan>
<!-- 第一种:org.mybatis.spring.SqlSessionTemplate用构造注入sqlSessionFactory
也许会觉奇奇怪:SqlSessionTemplate对应的就是session,使用这种方式每个dao都需要注入一个 SqlSessionTemplate
-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
Mybatis-Config.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>
<!--开启二级缓存 -->
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
</configuration>
建立dao包
VoteUserDao.java
package dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import entity.VoteUser;
public interface VoteUserDao {
//查询所有
public abstract List<VoteUser> SelectAllVoteUser();
}
VoteUserMapper.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="dao.VoteUserDao">
<cache></cache>
<!--查询所有 -->
<select id="SelectAllVoteUser" resultType="entity.VoteUser">
SELECT * FROM VOTEUSER
</select>
</mapper>
建立daoimpl包
VoteUserDaoImpl.java
package daoimpl;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import dao.VoteUserDao;
import entity.VoteUser;
//注册成bean供service调用
@Repository("voteUserDao")
public class VoteUserDaoImol implements VoteUserDao {
@Autowired
/*这里的sqlSessionTemplate相当于
SqlSessionFactoryBean(通过mybatis-config和数据源和xml构成的SqlSessionFactory类)
打开SqlSession的操作
把sqlSessionTemplate当session用
*/
private SqlSessionTemplate sqlSessionTemplate;
@Override
public List<VoteUser> SelectAllVoteUser() {
return sqlSessionTemplate.getMapper(VoteUserDao.class).SelectAllVoteUser();
}
//使用注入需要set方法
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
}
建立service包
VoteUserService.java
package service;
import java.util.List;
import entity.VoteUser;
public interface VoteUserService {
//查询所有
public abstract List<VoteUser> SelectAllVoteUser();
}
建立serviceimpl包
VoteUserServiceImpl.java
package serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.VoteUserDao;
import entity.VoteUser;
import service.VoteUserService;
@Service("voteUserService")
public class VoteUserServiceImpl implements VoteUserService{
@Autowired
private VoteUserDao voteUserDao;
//还是需要set
public VoteUserDao getVoteUserDao() {
return voteUserDao;
}
public void setVoteUserDao(VoteUserDao voteUserDao) {
this.voteUserDao = voteUserDao;
}
@Override
public List<VoteUser> SelectAllVoteUser() {
return voteUserDao.SelectAllVoteUser();
}
}
最后是测试包test
test01.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.VoteUser;
import service.VoteUserService;
public class test01 {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContent.xml");
VoteUserService voteUserService=(VoteUserService)applicationContext.getBean("voteUserService");
for (VoteUser user : voteUserService.SelectAllVoteUser()) {
System.out.println(user);
}
}
}