集成步骤
- 创建maven项目
- 加入maven依赖
- spring依赖
- mybatis依赖
- mysql驱动依赖
- spring事务依赖
- mybatis和spring集成依赖(mybatis官方提供的,用来在spring项目中创建mybatis的SqlSessionFactory,dao对象)
- 创建实体类
- 创建dao接口和mapper文件
- 创建mybatis主配置文件
- 创建Service接口和实现类,属性是dao
- 创建spring的配置文件:声明mybatis的对象交给spring创建
- 数据源
- SQLSessionFactory
- dao对象
- 声明自定义的service
- 创建测试类,获取Service对象,通过service调用dao完成数据库的访问
创建maven项目
略
加入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring的核心ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--spring做事务用到的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis和spring集成依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!--druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
创建实体类
package com.maj.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建dao接口和mapper文件
package com.maj.dao;
import com.maj.domain.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudents();
}
<?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.maj.dao.StudentDao">
<insert id="insertStudent">
insert into student values (#{id},#{name},#{age})
</insert>
<select id="selectStudents" resultType="com.maj.domain.Student">
select id,name,age from student order by id desc
</select>
</mapper>
创建mybatis主配置文件
<?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="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.maj.domain"/>
</typeAliases>
<mappers>
<package name="com.maj.dao"/>
</mappers>
</configuration>
创建Service接口和实现类
package com.maj.service;
import com.maj.domain.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
List<Student> queryStudents();
}
package com.maj.service.impl;
import com.maj.dao.StudentDao;
import com.maj.domain.Student;
import com.maj.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = null;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public List<Student> queryStudents() {
List<Student> students = studentDao.selectStudents();
return students;
}
}
创建spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--声明数据源DateSource,作用是连接数据库-->
<bean id="myDateSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- set注入给DruidDataSource 提供连接数据库信息 -->
<property name="url" value="jdbc:mysql://localhost:3306/springdb" />
<property name="username" value="root" />
<property name="password" value="maj6226543" />
<property name="maxActive" value="20" /> <!--最大连接数-->
</bean>
<!--声明mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--把数据库连接池赋值给dataSource属性-->
<property name="dataSource" ref="myDateSource"/>
<!--
mybatis主配置文件的位置
configLocation属性是Resource类型,读取配置文件
赋值使用value
指定路径名使用 classpath:
-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--创建dao对象,使用SqlSession的getMapper(StudentDao.class)
MapperScannerConfigurer: 在内部调用getMapper()生成每个dao接口的代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- sqlSessionFactoryBeanName属性指定 sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--
basePackage属性:
给指定包下的dao接口都执行一次getMapper()方法得到*所有对象*放在spring容器中
对象名是dao接口名的首字母小写,例如StudentDao==》studentDao
value值可以指定多个包(value="com.maj.dao1,com.maj.dao2,...")
-->
<property name="basePackage" value="com.maj.dao" />
</bean>
<bean id="studentService" class="com.maj.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean>
</beans>
从属性文件读取数据库连接信息
为了便于维护,可以将数据库连接信息写入到属性文件中,使 Spring 配置文件从中读取数据。
属性文件名称自定义,但一般都是放在 resources目录下。
Spring 配置文件从属性文件中读取数据时,需要在<property/>
的 value 属性中使用${ },将在属性文件中定义的 key 括起来,以引用指定属性的值。
<context:property-placeholder/>
方式:
-
该方式要求在 Spring 配置文件头部加入
spring-context.xsd
约束文件:xmlns:context="http://www.springframework.org/schema/context"
-
<context:property-placeholder/>
标签中有一个属性location
,用于指定属性文件的位置。
<context:property-placeholder location="classpath:jdbc.properties" />
<!--声明数据源DateSource,作用是连接数据库-->
<bean id="myDateSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- set注入给DruidDataSource 提供连接数据库信息 -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" /> <!--最大连接数-->
</bean>
创建测试类
package com.maj;
import com.maj.dao.StudentDao;
import com.maj.domain.Student;
import com.maj.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
/**
* 测试容器中存在的对象
*/
@Test
public void test01() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
String[] names = ac.getBeanDefinitionNames();
for (String name : names) {
System.out.println("容器中对象名称:" + name);
System.out.println(ac.getBean(name));
System.out.println("----------------------");
}
}
/**
* 测试dao
*/
@Test
public void test02() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
Student student = new Student(6, "惠子", 18);
int count = studentDao.insertStudent(student);
System.out.println("count=" + count);
}
/**
* 测试Service
*/
@Test
public void test03() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
StudentService studentService = (StudentService) ac.getBean("studentService");
Student student = new Student(7, "贞子", 18);
int count = studentService.addStudent(student);
System.out.println("count=" + count);
}
}