Spring整合Mybatis方式(MapperFactoryBean实现)

环境准备请看https://www.cnblogs.com/SweetheartAndPeaches/p/15468527.html


数据库准备

image


根据数据库建立entity包放一个VoteUser类

image

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配置

image

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&amp;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> 
       <!-- 第三种方法去掉impl实现类直接使用:
            org.mybatis.spring.mapper.MapperFactoryBean包帮你代理出一个接口实例出来在serciveimpl需要注入这个由MapperFactoryBean产生的bean
 -->
<bean id ="voteUserDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="dao.VoteUserMapper"></property>
<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包
(特别注意dao接口对应的xml名字需要相同MapperFactoryBean会找同名的xml)
image

VoteUserMapper.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>

建立service包

image

VoteUserService.java
  package service;

  import java.util.List;
  import entity.VoteUser;

  public interface VoteUserService {
  	 //查询所有
  		public abstract List<VoteUser> SelectAllVoteUser(); 
  }


建立serviceimpl包

image

VoteUserServiceImpl.java
  package serviceimpl;

  import java.util.List;

  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;

  import dao.VoteUserMapper;
  import entity.VoteUser;
  import service.VoteUserService;

  @Service("voteUserService")
  public class VoteUserServiceImpl implements VoteUserService{

  	@Autowired
   //原来这里注入的是daoimpl但是我们并没有写这个,由MapperFactoryBean产生proxy类
   //该类继承了VoteUserDao经过转型后注入
  	private  VoteUserMapper voteUserDao;
  	//还是需要set
  	public VoteUserDao getVoteUserDao() {
  		return voteUserDao;
  	}

  	public void setVoteUserDao(VoteUserDao voteUserDao) {
  		this.voteUserDao = voteUserDao;
  	}

  	@Override
  	public List<VoteUser> SelectAllVoteUser() {
  		return voteUserDao.SelectAllVoteUser();
  	}



  }


最后是测试包test

image

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);
  	}
  }
  }
  
posted @ 2021-10-27 16:44  旅祸少年  阅读(126)  评论(0编辑  收藏  举报