Mybatis入门案例
搭建Mybatis开发环境–基础CRUD操作实例
select
创建MySQL数据库和表
1 2 3 4 5 6 7
| CREATE TABLE `student` ( `id` int(11) NOT NULL , `name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
创建Maven工程,并且在pom.xml中加入maven坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> </dependencies>
|
加入maven插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <build/>
|
编写Student实体类
创建包com.bytedance.domain,包中创建Student类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.bjpowernode.domain;
public class Student { private Integer id; private String name; private String email; private Integer age; }
|
编写Dao接口 StudentDao
创建包com.bytedance.dao,创建StudentDao接口
1 2 3 4 5 6 7 8 9 10 11 12
| package com.bjpowernode.dao; import com.bjpowernode.domain.Student; import java.util.List;
public interface StudentDao { List<Student> selectStudents(); }
|
编写Dao接口Mapper映射文件StudentDao.xml
要求:
- 在 dao 包中创建文件 StudentDao.xml
- 要 StudentDao.xml 文件名称和接口 StudentDao 一样,区分大小写的一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?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.bytedance.dao.StudentDao">
<select id="selectStudents" resultType="com.bytedance.domain.Student"> select id,name,email,age from student </select> </mapper>
|
创建Mybatis主配置文件
项目 src/main 下创建 resources 目录,设置 resources 目录为 resources root 创建主配置文件:名称为 mybatis.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
| <?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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/>
<dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/bytedance/dao/StudentDao.xml"/> </mappers> </configuration>
|
创建测试类
src/test/java/com/bytedance/ 创建 MyBatisTest.java 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Test public void testStart() throws IOException { String config = "mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(config); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); List<Student> studentList = session.selectList("com.bytedance.dao.StudentDao.selectStudents"); studentList.forEach( student -> System.out.println(student)); session.close(); }
|
配置日志功能
mybatis.xml 文件加入日志配置,可以在控制台输出执行的 sql 语句和参数
1 2 3
| <settings> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings>
|
insert
StudentDao 接口中增加方法
1
| int insertStudent(Student student);
|
StudentDao.xml中加入SQL语句
1 2 3 4 5 6
| (2) StudentDao.xml 加入 sql 语句 <insert id="insertStudent"> insert into student(id,name,email,age) values(#{id},#{name},#{email},#{age}) </insert>
|
增加测试方法
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
| @Test public void testInsert() throws IOException { String config = "mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(config); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); Student student = new Student(); student.setId(1005); student.setName("张丽"); student.setEmail("zhangli@163.com"); student.setAge(20); int rows = session.insert( "com.bytedance.dao.StudentDao.insertStudent",student); session.commit(); System.out.println("增加记录的行数:"+rows); session.close();
}
|
update
StudentDao接口中增加方法
1
| int updateStudent(Student student);
|
StudentDao.xml文件中增加SQL语句
1 2 3
| <update id="updateStudent"> update student set age = #{age} where id=#{id} </update>
|
增加测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Test public void testUpdate() throws IOException { String config = "mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(config); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); Student student = new Student(); student.setId(1005); student.setAge(30); int rows = session.update( "com.bytedance.dao.StudentDao.updateStudent",student); session.commit(); System.out.println("修改记录的行数:"+rows); session.close(); }
|
delete
StudentDao接口中增加方法
1
| int deleteStudent(int id);
|
StudentDao.xml文件中增加SQL语句
1 2 3
| <delete id="deleteStudent"> delete from student where id=#{studentId} </delete>
|
增加测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Test public void testDelete() throws IOException { String config = "mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(config); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); int id = 1001; int rows = session.delete( "com.bytedance.dao.StudentDao.deleteStudent",id); session.commit(); System.out.println("修改记录的行数:"+rows); session.close(); }
|