Mybaits
学习总结(一)
前言
您可以本文了解到内容:
- 如何使用
IDEA
创建一个maven
工程配置基本Mybaits
- 如何通过
Mybaits
实现对单个表的增、删、改、查
创建maven
工程,配置pom.xml
-
创建
maven
工程,自定义项目名 -
配置
pom.xml
,引入依赖<dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <!-- 核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- mysql数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!--简化实体类的创建(setter、getter方法)--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <!-- 日志相关 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
以上代码:
org.mybatis
:是本项目的核心jar
,必须引入。mysql
:mysql的数据库依赖mysql
8.0 以上有变化。这里使用的版本是:8.0.15 加载驱动时对应配置需要改成:
com.mysql.cj.jdbc.Driver
读者也可结合自身使用5.1.xx版本 加载的驱动:
com.mysql.jdbc.Driver
junit
:单元测试的依赖,接下来测试功能时使用,省去写main
方法测试org.projectlombok
:通过在实体类前加@Data
注解,简化setter
、getter
方法的编写log4j
:打印日志相关信息,这里暂不使用必备的:
org.mybatis
、mysql
非必须:
org.projectlombok
、junit
、log4j
,这只是本人的编写习惯
创建数据库测试表
-
创建数据库表
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `userName` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `birthday` datetime DEFAULT NULL, `salary` decimal(10,0) DEFAULT NULL, `departId` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', '张三', '111', '11', '2020-06-12 16:12:36', '123', '1'); INSERT INTO `t_user` VALUES ('2', '李四', '222', '22', '2020-06-12 16:59:17', '100', '1'); INSERT INTO `t_user` VALUES ('3', '王五', '333', '22', '2020-06-12 16:59:52', '300', '2');
以上代码:在
Navicat
中复制运行即可创建出t_user
表
Mybaits
全局配置连接数据库
-
在
resources
下创建配置文件mybaits-config.xml
(文件名可以自定义) -
mybaits-config.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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/你的数据库名? useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CST"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- 这里先留空,之后再配置--> </mappers> </configuration>
以上代码说明:
-
<environment id="development">
:环境集合属性对象
-
<transactionManager type="JDBC"/>
:事务管理,采用数据库自带的事务来管理
mybaits
中jdbc
的事务 -
<dataSource type="POOLED">
:数据源,使用
mybatis
自带的数据连接池driver
:mysql
版本8.0以上:com.mysql.cj.jdbc.Driver
5.1.xx:com.mysql.jdbc.Driver
password
:改为你自己的数据库密码
-
<mappers>
:映射器,配置采用读取映射文件,注解采用注入接口
-
添加实体类、DAO接口
-
User
实体类@Data public class User { private Integer id;//用户id private String userName;//用户名 private String password;//mim private Integer age;//年龄 private Date birthday;//生日 private BigDecimal salary;//薪水 private Integer departId;//部门id }
以上代码:
@Data
注解基于lombok
依赖,简化setter、getter方法配置 -
IUserDao
接口public interface IUserDao { //查询所有用户 public List<User> queryAll(); //按id查询单个用户 public User queryone(Integer id); }
添加映射文件
注意:映射文件的路径要和Dao接口的路径相同
因为是文件夹,所以文件夹多层目录不能使用点。 需要用/ 比如
com/kingwan/mybatis/dao
文件夹创建完之后,创建一个file 命名IUserDao.xml
-
IUserDao.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.kingwan.mybaits.dao.IUserDao"> <resultMap id="resultMapUser" type="com.kingwan.mybaits.entity.User"> <id column="id" property="id"></id> <result column="userName" property="userName"></result> <result column="password" property="password"></result> <result column="age" property="age"></result> <result column="birthday" property="birthday"></result> <result column="salary" property="salary"></result> <result column="departId" property="departId"></result> </resultMap> <!--下面的的内容可以先不用配置--> <!--查询所有用户--> <select id="queryAll" resultMap="resultMapUser"> select * from t_user </select> <select id="queryone" resultMap="resultMapUser"> SELECT * FROM t_user WHERE id = #{id} </select> </mapper>
以上代码:
namespace
:命名空间,使用接口对应的全包名resultMap
:id代表结果集的名字,type代表结果集内对应的实体类(全类名)<id column="id" property="id"></id>
:对应数据库表主键<result column="userName" property="userName"></result>
:对应数据库表非主键属性