MyBatis的基本使用情况的一次记录
添加依赖
Maven 中添加如下依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency>
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.24</version> </dependency> </dependencies>
|
创建数据库和表
1 2 3 4 5 6 7 8 9 10
| CREATE DATABASE emp_database;
CREATE TABLE employee ( employee_id int, name text, salary numeric );
|
创建 employee 表对应的 POJO 类
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
| package cn.zzq.model;
public class Employee { private int employeeId; private String name; private float salary;
public Employee(int employeeId, String name, float salary) { this.employeeId = employeeId; this.name = name; this.salary = salary; }
public int getEmployeeId() { return employeeId; }
public void setEmployeeId(int employeeId) { this.employeeId = employeeId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public float getSalary() { return salary; }
public void setSalary(float salary) { this.salary = salary; } }
|
XML 配置
添加 MyBatis 的 XML 配置文件如下:
mybatis-config.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 configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/emp_database"/>
<property name="username" value="postgres"/> <property name="password" value="postgres"/> </dataSource> </environment> </environments>
<mappers> <mapper resource="mapper/employee.xml"/> </mappers> </configuration>
|
mapper 文件
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
| <?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="Employee">
<select id="selectAllEmployee" resultType="cn.zzq.model.Employee"> SELECT * FROM employee </select>
<insert id="insertEmployee" parameterType="cn.zzq.model.Employee"> INSERT INTO employee(employee_id, name, salary) VALUES (#{employeeId}, #{name}, #{salary}) </insert> <select id="selectPersonSalaryBiggerThan5000" resultType="cn.zzq.model.Employee"> SELECT * FROM employee WHERE salary > 5000 </select> </mapper>
|
编写主函数
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
| package cn.zzq;
import cn.zzq.model.Employee; 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 java.io.IOException; import java.io.InputStream; import java.util.List;
public class Main { public static void main(String[] args) throws IOException { String mybatisConfigPath = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(mybatisConfigPath); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession();
try { Employee employee = new Employee(1, "EmployTest", 23); sqlSession.selectOne("Employee.insertEmployee", employee); List<Employee> employeeList = sqlSession.selectList("Employee.selectAllEmployee"); System.out.println(employeeList);
sqlSession.selectOne("Employee.insertEmployee", new Employee(5, "Test", 6000)); sqlSession.selectOne("Employee.insertEmployee", new Employee(7, "test1", 5100));
List<Employee> persons1 = sqlSession.selectList("Employee.selectPersonSalaryBiggerThan5000"); System.out.println(persons1);
} finally { sqlSession.close(); } } }
|
输出结果
[Employee{employeeId=1, name=‘EmployTest’, salary=23.0}]
[Employee{employeeId=5, name=‘Test’, salary=6000.0}, Employee{employeeId=7, name=‘test1’, salary=5100.0}]
参考文献
MyBatis 官网中文入门指南