知识点
- 通过 Spring 整合 Mybatis
- pom 文件加入的坐标
- Spring 的 Mybatis 的整合包
- Spring 的 jdbc 整合包
- 第三方数据源 druid
- Spring 对 log4j 的配置
- Spring 配置文件
- 导入 properties 配置文件
- DruidDataSource
- SqlSessionFactoryBean
- MapperScannerConfigurer
代码示例
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>priv.noby</groupId>
<artifactId>spring3</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
</project>
Resource
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context http://www.springframework.org/schema/context/spring-context.xsd ">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="priv.noby.spring3.entity"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="priv.noby.spring3.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="studentService" class="priv.noby.spring3.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stage3?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
log4j.rootLogger=OFF
log4j.logger.priv.noby.spring3.dao=DEBUG,CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%p %c{1}:%L %m%n
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m [%t] %c [%l]%n
log4j.appender.FILE.File=file.log
entity
package priv.noby.spring3.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
}
dao
package priv.noby.spring3.dao;
import priv.noby.spring3.entity.Student;
public interface StudentDao {
Student selectById(int id);
boolean insert(Student student);
}
<?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="priv.noby.spring3.dao.StudentDao">
<select id="selectById" resultType="student">
select *
from student where id = #{id} </select>
<insert id="insert">
insert into student (name, age) value (#{name}, #{age})
</insert>
</mapper>
service
package priv.noby.spring3.service;
import priv.noby.spring3.entity.Student;
public interface StudentService {
Student selectById(int id);
boolean insert(Student student);
}
package priv.noby.spring3.service.impl;
import priv.noby.spring3.dao.StudentDao;
import priv.noby.spring3.entity.Student;
import priv.noby.spring3.service.StudentService;
public class StudentServiceImpl implements StudentService {
StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public Student selectById(int id) {
return studentDao.selectById(id);
}
@Override
public boolean insert(Student student) {
return studentDao.insert(student);
}
}
test
package priv.noby.spring3.dao;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import priv.noby.spring3.entity.Student;
public class StudentDaoTest {
private ApplicationContext applicationContext;
private StudentDao studentDao;
@Before
public void setUp(){
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
studentDao = (StudentDao) applicationContext.getBean("studentDao");
}
@Test
public void testSelectById() {
Student student = studentDao.selectById(1);
System.out.println("student = " + student);
}
@Test
public void testInsert() {
Student student = new Student();
student.setName("noby");
student.setAge(20);
boolean b = studentDao.insert(student);
System.out.println(b);
}
}
package priv.noby.spring3.service.impl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import priv.noby.spring3.entity.Student;
import priv.noby.spring3.service.StudentService;
public class StudentServiceImplTest {
private ApplicationContext applicationContext;
private StudentService studentService;
@Before
public void setUp() {
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
studentService = (StudentService) applicationContext.getBean("studentService");
}
@Test
public void testSelectById() {
Student student = studentService.selectById(1);
System.out.println("student = " + student);
}
@Test
public void testInsert() {
Student student = new Student();
student.setName("kace");
student.setAge(30);
boolean b = studentService.insert(student);
System.out.println(b);
}
}