个人主页:
✨集成的步骤
实现步骤
1.使用的mysql库, 使用学生表 student2(id int 主键列, 自动增长,
name varchar(80),
age int)2.创建maven项目
3.加入依赖gav
spring依赖, mybatis依赖, mysql驱动。 junit依赖
mybatis-spring依赖(mybatis网站上提供的,用来在spring项目中,创建mybatis对象)
spring有关事务的依赖。mybatis和spring整合的时候, 事务是自动提交的。
4.创建实体类Student
5.创建Dao接口和mapper文件写sql语句
6.写mybatis主配置文件
7.创建service接口和他的实现类
8.创建spring的配置文件
1)声明数据源DataSource,使用的阿里的Druid连接池
2) 声明SqlSessionFactoryBean类,在这个类内部创建的是SqlSessionFactory对象。
3)声明MapperScannerConfiguration类,在内部创建dao代理对象,创建的对象都放在spring容器中。
4)声明Service对象,把3)的中dao赋值给service属性9.测试dao访问数据库
✨创建数据库表
这里简单的创建一下数据库表
✨pom加入依赖
<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>youfei1_v</groupId> <artifactId>ch08-spring-mybatis</artifactId> <version>1.0.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <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> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build></project>
✨创建MyBatis使用代码
首先是创建实体类,生成get、set和toString方法
public class Student { private Integer id; private String name; private Integer 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; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; }}
然后创建Dao接口写方法
public interface StudentDao { //添加操作 int insertStudent(Student student); //查询操作 List<Student> selectStudents();}
创建mapper文件(这里我起的文件名为StudentDao.xml)
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="youfei1_v.Dao.StudentDao"> <insert id="insertStudent" > insert into student2(name,age) values (#{name},#{age}) </insert> <select id="selectStudents" resultType="youfei1_v.domain.Student"> select id,name ,age from student2 </select></mapper>
创建mybatis主配置文件(这里为给文件名起的为mybatis.xml)
<!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> <mappers> <!----> <package name="youfei1_v.Dao"/> </mappers></configuration>
✨创建Service类
首先创建Service接口
public interface StudentService { int addStudent(Student student); List<Student> queryStudent();}
实现这个接口
public class StudentServiceImpl implements StudentService { private StudentDao studentDao; //创建这个dao对象和set方法 public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } @Override public int addStudent(Student student) { int rows = studentDao.insertStudent(student); //方法中调用studentDao对象的方法 return rows; } @Override public List<Student> queryStudent() { List<Student> students = studentDao.selectStudents(); return students; }}
✨创建Spring配置文件
这里我给Spring配置文件的文件名起名为applicationContext.xml
<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 https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/springdb"/> <property name="username" value="root"/> <property name="password" value="297744"/> </bean> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="factory" /> <property name="basePackage" value="youfei1_v.Dao" /> </bean> <bean id="studentService" class="youfei1_v.service.impl.StudentServiceImpl"> <property name="studentDao" ref="studentDao" /> </bean> </beans>
✨测试集成MyBatis
✨使用外部属性配置文件
投票传送门(欢迎伙伴们投票)