Integration of spring+mybatis

1. Project structure diagram
Insert picture description here2. Jar package
1. MySQL database driver version must be 5.1.46 and above
2. The red box is the necessary
Insert picture description here
spring configuration file
beans.xml of the jar package
1. Placeholder ${}, don’t Wrong writing
2. The <?xml version="1.0" encoding="UTF-8"?> of the xml file must be on the first line, otherwise an error will be reported
3. There can be no spaces between the driver and the URL
4. The database username and password are required Write right

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 
 
 <!-- 加载do.properties文件 -->
<context:property-placeholder location="classpath:do.properties"/>
         
         
         <!-- 配置数据源 -->
 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 

<!-- 数据库驱动 -->
     <property name="driverClassName" value="${jdbc.driver}" /> 

<!-- 连接字符串 -->
     <property  name="url"  value="${jdbc.url}" />  
 
 <!-- 数据库用户名和密码 -->
     <property name="username" value="${jdbc.username}" /> 
	 <property name="password" value="${jdbc.password}" />
	
	 <property name="maxTotal" value="${jdbc.maxTotal}" /> 
	 <property name="maxIdle" value="${jdbc.maxIdle}" />
	 <property name="initialSize" value="${jdbc.initialSize}" /> 
	  
 </bean> 
 
  <!-- 声明事务管理器,依赖于数据源 -->
  <bean 
         id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  		
  		<property name="dataSource" ref="dataSource" /> 
  
  </bean>
  
   <!-- 基于annotation(注解)的声明式事务 -->
  <tx:annotation-driven transaction-manager="transactionManager" />
  
  
  <!-- 配置mybatis工厂 -->
  <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
          
          <!-- 注入数据源 -->
          <property name="dataSource" ref="dataSource" />
          
          <!-- 加载mybatis配置文件 -->
          <property name="configLocation"
                    value="classpath:mybatis-config.xml" /> 
 
  </bean>
  
  <!--   传统Dao模式开发--> 
 <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"> 

	<property name="sqlSessionFactory" ref="sqlSessionFactory" /> 

</bean>

 
<bean id="customerMapper" 
class="org.mybatis.spring.mapper.MapperFactoryBean">
	
	<!--  value=包+接口 -->
	<property name="mapperInterface"
	          value="com.mapper.CustomerMapper"/>
	          
	<property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean>

<!--  mapper代理开发(基于MapperScannerConfigurer)-->

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       
       <!-- 加载包;value=包名(不要写到类);不必引用sqlSessionFactory-->
       <property name="basePackage" value="com.mapper" />
 </bean>

</beans>         

Mapper (MFB and MSC) agent development notes and specifications:
1. The name of the mapper interface must be consistent with the file mapped by the mapper.
2. The namespace in the Mapper.xml file is the same as the class path of the Mapper interface (that is, the interface file and the mapping The file needs to be placed in the same package)
If the above two are followed, then you don’t need to introduce mapper.xml in mybatis-config
3. The method name in the Mapper interface should be consistent with each execution statement defined in mapper.xml
4. The parameter should be consistent with parameterType
5. The return type should be consistent with resultType

do.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/emis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30 
jdbc.maxIdle=10
jdbc.initialSize=5

mybatis-conlig.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>
	<mappers>	  
		  <mapper resource="mapper/Customer.xml"/> <!-- 加载映射文件 -->      
	</mappers>
</configuration>

Mapping file Customer.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="mc">
   
   <select id="findID"  parameterType="Integer"
           resultType="com.itheima.po.Customer">
    select * from customer where id=#{id}
   </select>
   

 </mapper>

Entity class
attribute name should be consistent with the attribute name in the database

package com.itheima.po;

public class Customer {
	
	private int id;
	private String name;
	private String jobs;
	private String phone;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getJobs() {
		return jobs;
	}
	public void setJobs(String jobs) {
		this.jobs = jobs;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String toString()
	{
		return id+","+name+","+jobs+","+phone+"\n";	
	}
}

//Dao接口
package com.itheima.dao;

import com.itheima.po.Customer;

public interface CustomerDao {
	
	public Customer findID(Integer id);

}

//Dao实现类
package com.itheima.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;

public class CustomerDaoImpl extends SqlSessionDaoSupport
implements CustomerDao{

	@Override
	public Customer findID(Integer id) {
		return 
		this.getSqlSession().selectOne
		("mc.findID", id); //命名空间+SQL执行语句的id
	}

}

Test class


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
import com.mapper.CustomerMapper;


public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ApplicationContext applicationContext = new
				ClassPathXmlApplicationContext("beans.xml");
        
		//传统的Dao模式
		//根据容器中的id获取bean
//		CustomerDao cd = (CustomerDao) applicationContext.getBean("customerDao");
//        
//		Customer c = cd.findID(1);
		
//		mapper接口开发(MapperFactoryBean和MapperScannerConfigurer类)
		
//      通过className.class获取bean
		CustomerMapper cm =  applicationContext.getBean(CustomerMapper.class);
		
		Customer c = cm.findId(1);
		System.out.println(c);
	}

}

Guess you like

Origin blog.csdn.net/m0_46267375/article/details/106481228