mybatis的使用-单独使用mybatis
一、单独使用mybatis
参照mybatis官方文档 https://mybatis.org/mybatis-3/zh/getting-started.html
整体代码结构:
1.使用eclipse新建一个maven项目,将项目的java编译器版本修改为1.8。
或者通过修改pom.xml文件,增加以下内容将编译修改为1.8:
<build> <finalName>income</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
2.在pom.xml文件中增加mybatis依赖和mysql依赖。
pom.xml文件内容:
<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>com.test</groupId> <artifactId>mybatisdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatisdemo</name> <url>http://maven.apache.org</url> <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>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>income</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
3.新建BootStrap.java类
package com.test.mybatisdemo; import java.io.IOException; import java.io.InputStream; 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 com.test.mybatisdemo.entity.User; import com.test.mybatisdemo.mapper.UserMapper; public class BootStrap { public static void main( String[] args ) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); try(SqlSession session = sqlSessionFactory.openSession()) { //使用mybatis的API,旧版本mybatis中常见 User user1 = session.selectOne("com.test.mybatisdemo.mapper.UserMapper.getUserById",1); System.out.println(user1.getId() + "," + user1.getUserName() + "," + user1.getPassword() + "," + user1.getCreateTime()); //使用Mapper方法,代码结构更清晰,更容易理解 UserMapper userMapper = session.getMapper(UserMapper.class); User user = userMapper.getUserById(1); System.out.println(user.getId() + "," + user.getUserName() + "," + user.getPassword() + "," + user.getCreateTime()); } } }
4.为了将配置文件和代码文件分开存放,新增一个sources资源目录,用于存放mybatis-config.xml
5.配置mybatis-config.xml文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.jdbc.Driver"/> 11 <property name="url" value="jdbc:mysql://localhost:3306/test"/> 12 <property name="username" value="root"/> 13 <property name="password" value="111111"/> 14 </dataSource> 15 </environment> 16 </environments> 17 18 <mappers> 19 <mapper resource="com/test/mybatisdemo/mapper/UserMapper.xml"/> 20 <!-- <package name="com/test/mybatisdemo/mapper"/> --> 21 </mappers> 22 </configuration>
使用了方便增加多个mapper,配置mapper时,可以使用package指定命名空间,减少mapper的配置。
6.创建user表并插入一条测试数据
1 create table user 2 ( 3 id int(10), 4 user_name varchar(20), 5 password varchar(32), 6 create_time datetime, 7 primary key(id) 8 ) 9 10 insert into user values(1,'测试用户','123456','2021-10-01 08:08:08');
7.创建User.java实体类
1 package com.test.mybatisdemo.entity; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; 7 private String userName; 8 private String password; 9 private Date createTime; 10 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getUserName() { 18 return userName; 19 } 20 public void setUserName(String userName) { 21 this.userName = userName; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 public Date getCreateTime() { 30 return createTime; 31 } 32 public void setCreateTime(Date createTime) { 33 this.createTime = createTime; 34 } 35 36 37 }
如果不想手工增加setter和getter方法,可以使用lombok。
8.创建mapper接口,即UserMapper.java接口
1 package com.test.mybatisdemo.mapper; 2 3 import com.test.mybatisdemo.entity.User; 4 5 public interface UserMapper { 6 7 User getUserById(Integer id); 8 9 }
该接口不需要实现类,由mybatis自动生成该接口的代理类。
9.创建mapper的配置文件,即UserMapper.xml.
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.test.mybatisdemo.mapper.UserMapper"> 6 <resultMap type="com.test.mybatisdemo.entity.User" id="userMap"> 7 <id column="id" property="id"/> 8 <result column="user_name" property="userName"/> 9 <result column="password" property="password"/> 10 <result column="create_time" property="createTime"/> 11 </resultMap> 12 13 <select id="getUserById" resultMap="userMap"> 14 select * from user where id = #{id} 15 </select> 16 </mapper>
由于user表的字段名和User.java类属性名不一致,mybatis无法识别并进行映射,因此,需要配置resultMap进行结果映射。
resultMap的属性,参照:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html
constructor
- 用于在实例化类时,注入结果到构造方法中idArg
- ID 参数;标记出作为 ID 的结果可以帮助提高整体性能arg
- 将被注入到构造方法的一个普通结果
id
– 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能result
– 注入到字段或 JavaBean 属性的普通结果association
– 一个复杂类型的关联;许多结果将包装成这种类型- 嵌套结果映射 – 关联可以是
resultMap
元素,或是对其它结果映射的引用
- 嵌套结果映射 – 关联可以是
collection
– 一个复杂类型的集合- 嵌套结果映射 – 集合可以是
resultMap
元素,或是对其它结果映射的引用
- 嵌套结果映射 – 集合可以是
discriminator
– 使用结果值来决定使用哪个resultMap
case
– 基于某些值的结果映射- 嵌套结果映射 –
case
也是一个结果映射,因此具有相同的结构和元素;或者引用其它的结果映
- 嵌套结果映射 –
链接:https://pan.baidu.com/s/13nCPX0RkEbfhHWBbU4YyOQ
提取码:i2x1