SSM-8-整合MyBatis和Spring
准备工作
- 准备jar包
 - 编写配置文件
- db.properties
 - applicationContext
 - mybatis-config
 - log4j.properties
 
 
jar包
ant-1.9.6.jar
ant-launcher-1.9.6.jar
asm-5.1.jar
aspectjweaver-1.8.10.jar
cglib-3.2.4.jar
commons-dbcp2-2.1.1.jar
commons-logging-1.2.jar
commons-pool2-2.4.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.40-bin.jar
mysql-connector-java-5.1.8.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar
配置文件
db.properities
1 2 3 4 5 6 7
   | jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.134.128:3306/mybatis jdbc.username=root jdbc.password=root jdbc.maxTotal=30 jdbc.maxIdle=10 jdbc.initialSize=5
   | 
applicationContext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   | <?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/context      http://www.springframework.org/schema/context/spring-context-4.3.xsd     http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  	<context:property-placeholder 		location="classpath:db.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> 	 	<tx:annotation-driven 		transaction-manager="transactionManager" /> 	 	 	<bean id="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> 		 		<property name="dataSource" ref="dataSource" /> 		 		<property name="configLocation" 			value="classpath:mybatis-config.xml"></property> 	</bean> </beans>
   | 
mybatis-config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | <?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> 	 	 	<typeAliases> 		 		 		<package name="com.itheima.po"/> 	</typeAliases>
  	 	<mappers> 		<mapper resource="com/itheima/mapper/IdCardMapper.xml" /> 		<mapper resource="com/itheima/mapper/PersonMapper.xml" /> 		<mapper resource="com/itheima/mapper/UserMapper.xml" /> 		<mapper resource="com/itheima/mapper/OrdersMapper.xml" /> 		<mapper resource="com/itheima/mapper/ProductMapper.xml" /> 	</mappers> </configuration>
   | 
log4j.properties
1 2 3 4 5 6 7 8
   |  log4j.rootLogger=ERROR, stdout
  log4j.logger.com.itheima=DEBUG
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
 
  | 
新建package
com.itheima
传统Dao方式的开发整合
Customer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   | package com.itheima.po;
 
 
 
 
  public class Customer { 	private Integer id;  	private String username;  	private String jobs;  	private String phone; 
  	public Integer getId() { 		return id; 	}
  	public void setId(Integer id) { 		this.id = id; 	}
  	public String getUsername() { 		return username; 	}
  	public void setUsername(String username) { 		this.username = username; 	}
  	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; 	}
  	@Override 	public String toString() { 		return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]"; 	} }
   | 
CustomerMapper
1 2 3 4 5 6 7 8 9 10 11
   | <?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.itheima.mapper.CustomerMapper"> 	 	<select id="findCustomerById" parameterType="Integer" resultType="customer"> 		select * from t_customer where id = #{id} 	</select> </mapper>
   | 
CustomerDao
1 2 3 4 5 6 7 8 9 10 11 12
   | package com.itheima.dao;
  import com.itheima.po.Customer;
 
 
 
 
 
  public interface CustomerDao { 	public Customer findCustomerById(Integer id); }
   | 
CustomerImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | 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 findCustomerById(Integer id) { 		return this.getSqlSession().selectOne("com.itheima.mapper.CustomerMapper.findCustomerById", 1); 	} 	 }
   | 
applicationContext需要将customerDao注入SqlSession
1 2 3
   | <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"> 	<property name="sqlSessionFactory" ref="SqlSessionFactory"></property> </bean>
   | 
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   | package com.itheima.test;
  import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
  import com.itheima.dao.CustomerDao; import com.itheima.po.Customer;
 
 
 
 
 
  public class DaoTest { 	@Test 	public void findCustomerByIdDaoTest() { 		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 		 		CustomerDao customerDao = applicationContext.getBean(CustomerDao.class); 		Customer customer = customerDao.findCustomerById(1); 		System.out.println(customer); 	} }
   | 
基于Mapper接口方式的开发整合
- mapperInterface 用于指定接口
 - SqlSessionFactory 用于指定SqlSessionFactory
 - SqlSessionTemplate 用于指定SqlSessionTemplate 如果跟2同时使用,则只用3
 
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | <?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.itheima.mapper.CustomerMapper"> 	 	<select id="findCustomerById" parameterType="Integer" resultType="customer"> 		select * from t_customer where id = #{id} 	</select> </mapper>
   | 
applicationContext
1 2 3 4 5 6
   |  <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 	<property name="mapperInterface" value="com.itheima.mapper.CustomerMapper"/> 	<property name="sqlSessionFactory" ref="SqlSessionFactory"/> 	 </bean>
 
  | 
- basePackage 指定映射接口文件所在路径
 - annotationClass
 - sqlSessionFactoryBeanName
 - sqlSessiontemplateBeanName
 - markerInterface
 
applicationContext
1 2 3 4
   |  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 	<property name="basePackage" value="com.itheima.mapper"/> </bean>
 
  | 
mybatis-config里mapper文件可以删掉
同时也要满足上一节的三个条件
事务测试
注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | package com.itheima.service.impl;
  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
  import com.itheima.mapper.CustomerMapper; import com.itheima.po.Customer; import com.itheima.service.CustomerService;
 
 
 
 
 
 
  @Service @Transactional public class CustomerServiceImpl implements CustomerService{ 	 	@Autowired 	private CustomerMapper customerMapper; 	@Override 	public void addCustomer(Customer customer) { 		this.customerMapper.addCustomer(customer); 		 	} 	 }
   | 
本文作者:NoOne
本文地址: https://noonegroup.xyz/posts/4389218b/
版权声明:转载请注明出处!