环境
系统 :Win10
软件 :Eclipse(需要配置了Maven环境)
开始
数据库
如果自己有数据库可以跳过这个步骤,这里我的数据库对应的是我这个示例
下面给出基于Navicat Premium 15的SQL转储文件,用软件这个mybatisdb.sql
执行即可创建
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 SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0 ;DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `Sno` char (8 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `Sname` varchar (10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , PRIMARY KEY (`Sno`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic ; SET FOREIGN_KEY_CHECKS = 1 ;
配置
提前看项目结构
创建项目
首先在Eclipse新建项目选择Maven Project
,按引导填入包名项目名等,无特殊设置
项目建立完成后找到pom.xml
文件,打开,并写入配置
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 <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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > db</groupId > <artifactId > mybatisapp</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > mybatisapp</name > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > </properties > <dependencies > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.7</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.15</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.9</version > <scope > test</scope > </dependency > </dependencies > </project >
在src/main/java
包下新建三个文件 和两个文件夹 ,并且在两个文件夹下也新建如下文件
文件结构
文件结构
其中**configuration.xml
** 是最重要的Mybatis配置文件,db.properties
为数据库的配置信息文件
我们先开始写 configuration.xml
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 <?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 > <properties resource ="db.properties" /> <environments default ="mysql" > <environment id ="mysql" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${driver}" /> <property name ="url" value ="${url}" /> <property name ="username" value ="${username}" /> <property name ="password" value ="${password}" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="cn/mydb/dao/StudentDaoMapper.xml" /> </mappers > </configuration >
db.properties
如下:
1 2 3 4 5 6 7 driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql: username=root password=123456
其中四个变量 根据你自己的数据库信息来配置
创建
现在开始写实体类,实体类的类名 需要跟数据库对应的表名 相同,且类属性 也要与表中列 一一对应,包括名字 和数据类型
Student.java
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 package cn.mydb.entity;public class Student { private String Sno; private String Sname; public String getSno () { return Sno; } public String getSname () { return Sname; } public void setSno (String sno) { Sno = sno; } public void setSname (String sname) { Sname = sname; } public String toString () { return "Student [Sno=" + Sno + ", Sname=" + Sname + "]" ; } }
现在来创建对应这个实体的实体映射接口 文件StudentDao.java
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 package cn.mydb.dao;import cn.mydb.entity.Student;public interface StudentDao { Student findStudentBySno (String Sno) ; void addUser (Student user) ; void deleteUser (String Sno) ; void updateUser (Student user) ; }
在之前的对Configuration.xml
的配置中有一项Mapper的配置,对应我们现在要写的映射文件
StudentDaoMapper.xml
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 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="cn.mydb.dao.StudentDao" > <select id ="findStudentBySno" parameterType ="java.lang.String" resultType ="cn.mydb.entity.Student" > select * from student where Sno = #{0} </select > <insert id ="addUser" parameterType ="cn.mydb.entity.Student" useGeneratedKeys ="true" keyProperty ="id" > insert into student(Sno,Sname) values(#{Sno},#{Sname}) </insert > <delete id ="deleteUser" parameterType ="java.lang.String" > delete from student where Sno = #{0} </delete > <update id ="updateUser" parameterType ="cn.mydb.entity.Student" > update student set Sname = #{Sname} where Sno = #{Sno} </update > </mapper >
这个映射文件是跟你的实体映射接口 所对应的,可以对比来了解里面各个参数的意义,或查询官方文档
到此我们服务端的部分就编写完成了
测试
我们引入了一个用于单元测试的包Junit
前面已经在Maven里导入了。我们直接编写一个单元测试类
StudentDaoTest.java
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 55 56 57 58 59 60 61 62 package cn.mydb;import java.io.IOException; import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import cn.mydb.dao.StudentDao;import cn.mydb.entity.Student;public class StudentDaoTest { @Test public void findStudentBySno () { SqlSession sqlSession = getSessionFactory().openSession(true ); StudentDao userMapper = sqlSession.getMapper(StudentDao.class); Student user = new Student (); user.setSname("pan" ); user.setSno("18115296" ); userMapper.addUser(user); user.setSname("panyajie" ); userMapper.updateUser(user); String sno = user.getSno(); user = null ; user = userMapper.findStudentBySno(sno); System.out.println("更新后记录为:" +user); System.out.println("尝试删除该记录..." ); userMapper.deleteUser(sno); user = userMapper.findStudentBySno(sno); if (user==null ){ System.out.println("该记录已删除!" ); }else { System.out.println("该记录未被成功删除!" ); } } private static SqlSessionFactory getSessionFactory () { SqlSessionFactory sessionFactory = null ; String resource = "cn/mydb/configuration.xml" ; try { sessionFactory = new SqlSessionFactoryBuilder ().build(Resources.getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } }
我们直接运行这个测试类,即可看到结果