首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
第一章:增删改查
第二章:INSERT 获取自增主键的值
第三章:INSERT 获取非自增主键的值
第四章:参数处理
4.1 单个参数
4.2 多个参数
4.3 命名参数
4.4 POJO
4.5 Map
4.6 TO
4.7 Collection 类型:List、Set、数组
4.8 # 和 $ 取值区别
4.9 #取值时指定参数的相关规则
第五章:select 元素
5.1 概述
5.2 resultType
5.3 resultMap
当前位置:
首页>>
技术小册>>
Mybatis合辑2-Mybatis映射文件
小册名称:Mybatis合辑2-Mybatis映射文件
- 示例: - 导入相关 jar 包的 Maven 坐标: ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> ``` - db.properties ``` jdbc.url=jdbc:mysql://192.168.134.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.username=root jdbc.password=123456 ``` - log4j.xml ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> ``` - Employee.java ``` package com.sunxiaping.domain; import org.apache.ibatis.type.Alias; @Alias("emp") public class Employee { private Integer id; private String lastName; private String email; private String gender; public Employee() { } public Employee(Integer id, String lastName, String email, String gender) { this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Employee{" + "id=" + id + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", gender='" + gender + '\'' + '}'; } } ``` - EmployeeMapper.java ``` package com.sunxiaping.mapper; import com.sunxiaping.domain.Employee; public interface EmployeeMapper { /** * 根据id查询员工的信息 * * @param id * @return */ Employee findById(Integer id); /** * 增加员工信息 * * @param employee */ void addEmp(Employee employee); /** * 修改员工信息 * * @param employee */ void updateEmp(Employee employee); /** * 根据id删除员工信息 * * @param id */ void deleteEmpById(Integer id); } ``` - EmployeeMapper.xml ``` <?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.sunxiaping.mapper.EmployeeMapper"> <select id="findById" resultType="com.sunxiaping.domain.Employee"> SELECT id,last_name as lastName,email,gender FROM employee WHERE id = #{id,jdbcType=INTEGER} </select> <insert id="addEmp"> INSERT INTO employee (last_name,email,gender) VALUES (#{lastName,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},#{gender,jdbcType=VARCHAR}) </insert> <update id="updateEmp"> UPDATE employee SET last_name = #{lastName,jdbcType=VARCHAR},email = #{email,jdbcType=VARCHAR},gender=#{gender,jdbcType=VARCHAR} WHERE id = #{id,jdbcType=INTEGER} </update> <delete id="deleteEmpById"> DELETE FROM employee WHERE id = #{id,jdbcType=INTEGER} </delete> </mapper> ``` - mybatis-copnfig.xml ``` <?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"></properties> <settings> <!-- 开启自动驼峰命名规则映射 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 使用${}引入db.properties属性文件中的内容 --> <property name="driver" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.sunxiaping.mapper"/> </mappers> </configuration> ``` - 测试: ``` package com.sunxiaping; import com.sunxiaping.domain.Employee; import com.sunxiaping.mapper.EmployeeMapper; 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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; public class EmployeeTest { SqlSessionFactory sqlSessionFactory = null; SqlSession sqlSession = null; EmployeeMapper employeeMapper = null; @Before public void before() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(true); employeeMapper = sqlSession.getMapper(EmployeeMapper.class); } @After public void after() { if (null != sqlSession) { sqlSession.close(); } } @Test public void testFindById() { Employee employee = employeeMapper.findById(1); System.out.println("employee = " + employee); } @Test public void testAddEmp() { Employee employee = new Employee(); employee.setEmail("jerry@qq.com"); employee.setGender("男"); employee.setLastName("jerry"); employeeMapper.addEmp(employee); } @Test public void testUpdateEmp(){ Employee employee = employeeMapper.findById(2); employee.setGender("女"); employeeMapper.updateEmp(employee); } @Test public void testDeleteEmpById(){ employeeMapper.deleteEmpById(2); } } ```
下一篇:
第二章:INSERT 获取自增主键的值
该分类下的相关小册推荐:
java源码学习笔记
Java语言基础13-类的加载和反射
深入拆解 Java 虚拟机
Java语言基础6-面向对象高级
Mybatis合辑3-Mybatis动态SQL
Java必知必会-Maven高级
Java语言基础11-Java中的泛型
手把手带你学习SpringBoot-零基础到实战
Java语言基础8-Java多线程
Java语言基础1-基础知识
Java语言基础12-网络编程
经典设计模式Java版