Spring整合Mybatis方式(继承SqlSessionDaoSupport类实现)
环境准备请看: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>
<!-- 第二种方法不需要SqlSessionTemplate,却需要daoimpl的实现类继承SqlSessionDaoSupport类、这个类有一个属性叫SqlSessionFactory需要注入
然后daoimpl的实现类就可以通过this.getSession()拿到session。
-->
<bean id="voteUserDao" class="daoimpl.VoteUserDaoImol">
<property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
</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包(注意区别SqlSessionTemplate方法)
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;
//这里使用xml方法注入,而不再是注解方法注入
public class VoteUserDaoImol extends SqlSessionDaoSupport implements VoteUserDao {
//继承SqlSessionDaoSupport类这个类有一个自带的属性叫SqlSessionFactory需要注入,此时自动装配失效
@Override
public List<VoteUser> SelectAllVoteUser() {
return this.getSqlSession().getMapper(VoteUserDao.class).SelectAllVoteUser();
}
}
建立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);
}
}
}