Mybatis注解开发
一、MyBatis的常用注解类型


package com.ithiema.Dao; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Builder public class User { private int id; private String username; private String password; private String perms; }
package com.ithiema.Mapper; import com.ithiema.Dao.User; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import java.util.List; public interface UserMapper { @Insert("INSERT INTO user(username,password,perms) VALUES(#{username},#{password},#{perms})") public void save(User user); @Select("SELECT * FROM user") public List<User> findAll(); @Select("SELECT id,username,password,perms FROM user WHERE id = #{id}") public User findById(int id); @Delete("DELETE FROM user WHERE id=#{id}") public void delete(int id); @Update("UPDATE user SET username=#{username},password=#{password},perms=#{perms} WHERE id=#{id}") public void update(User user); }
driver=com.mysql.cj.jdbc.Driver username=root password=admin url=jdbc:mysql://localhost:3306/dbstudy?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
<?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 -> settings -> typeAliases -> typeHandlers -> environments -> mappers --> <!-- 1.加载外部properties数据库配置文件 --> <properties resource="jdbc.properties"> <!-- 也可以直接写property --> </properties> <!-- 2.全局设置 --> <settings> <setting name="logImpl" value="SLF4J"/> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 3.起别名,简化mapper中实体类全限定名 --> <typeAliases> <typeAlias type="com.ithiema.Dao.User" alias="user"></typeAlias> </typeAliases> <!-- 4.多环境配置,default指定使用哪个环境 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 5.注册Mapper映射文件(核心!) --> <mappers> <!-- 方式1:resource加载xml --> <!-- <mapper resource="com.ithiema.mapper/UserMapper.xml"/>--> <!-- 方式2:扫描整个包,接口名和xml同名同路径 --> <package name="com.ithiema.Mapper"/> </mappers> </configuration>
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId> <artifactId>Mybatis_anno</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Mybatis_anno Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- MyBatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.19</version> </dependency> <!-- MySQL8驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency> <!-- logback,SLF4J实现,不用log4j --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.11</version> </dependency> <!-- Junit4 测试用(可选,写单元测试) --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.46</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>Mybatis_anno</finalName> </build> </project>
二、注解实现复杂映射开发

package com.ithiema.Mapper; import com.ithiema.Dao.Order; import com.ithiema.Dao.User; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import java.util.List; public interface OrderMapper { @Select("select *,o.id oid from orders o,user u where o.uid=u.id") // @Results({ // @Result(column = "oid", property = "id"), // @Result(column = "total", property = "total"), // @Result(column = "uid", property = "user.id"), // @Result(column = "username", property = "user.username"), // @Result(column = "password", property = "user.password"), // }) @Results({ @Result(column = "oid", property = "id"), @Result(column = "total", property = "total"), @Result( property = "user",//要封装的属性的名称 column = "uid",//根据哪个字段去查询user表的数据 javaType = User.class,//要封装的实体类型 //select属性,代表 查询 的哪个接口的方法获得数据 one = @One(select = "com.ithiema.Mapper.UserMapper.findById") ) }) public List<Order> findAll(); }
一对多查询
package com.ithiema.Mapper; import com.ithiema.Dao.Order; import com.ithiema.Dao.User; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import java.util.List; public interface OrderMapper { @Select("select *,o.id oid from orders o,user u where o.uid=u.id") // @Results({ // @Result(column = "oid", property = "id"), // @Result(column = "total", property = "total"), // @Result(column = "uid", property = "user.id"), // @Result(column = "username", property = "user.username"), // @Result(column = "password", property = "user.password"), // }) @Results({ @Result(column = "oid", property = "id"), @Result(column = "total", property = "total"), @Result( property = "user",//要封装的属性的名称 column = "uid",//根据哪个字段去查询user表的数据 javaType = User.class,//要封装的实体类型 //select属性,代表 查询 的哪个接口的方法获得数据 one = @One(select = "com.ithiema.Mapper.UserMapper.findById") ) }) public List<Order> findAll(); @Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid); }
OrderMapper加一句 以供调用
@Select("select * from orders where uid=#{uid}")
public List<Order> findByUid(int uid);
package com.ithiema.Mapper; import com.ithiema.Dao.User; import org.apache.ibatis.annotations.*; import java.util.List; public interface UserMapper { @Insert("INSERT INTO user(username,password,perms) VALUES(#{username},#{password},#{perms})") public void save(User user); @Select("SELECT * FROM user") public List<User> findAll(); @Select("SELECT id,username,password,perms FROM user WHERE id = #{id}") public User findById(int id); @Delete("DELETE FROM user WHERE id=#{id}") public void delete(int id); @Update("UPDATE user SET username=#{username},password=#{password},perms=#{perms} WHERE id=#{id}") public void update(User user); @Select("select * from user") @Results({ @Result( id=true, property = "id", column = "id"),//id=true表明是主键id @Result(column = "username",property = "username"), @Result(column = "password", property = "password"), @Result( property = "orderList", column = "id", javaType = List.class, many = @Many(select = "com.ithiema.Mapper.OrderMapper.findByUid") ) }) public List<User> findUserAndOrder(String username); }
多对多查询
图表见下方链接:https://blog.csdn.net/m0_57532232/article/details/164811038?spm=1001.2014.3001.5501
@Select("select * from user")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "username",property = "username"),
@Result(column = "password",property = "password"),
@Result(
property = "roleList",
column = "id",
javaType = List.class,
many = @Many(select = "com.ithiema.Mapper.RoleMapper.findByUid")
)
})
public List<User> findUserAndRole
package com.ithiema.Mapper; import org.apache.ibatis.annotations.Select; public interface RoleMapper { @Select("select * from sys_user_role ur,sys_role r where ur.roleId=r.id AND ur.userId=#{uid}") public List<Role> findByUid(); }
相对于一对多,一对一的查询,一对多查询多了一张中间表,即:将userid 与 roleid 对应的中间表。先将中间表和职能表进行链接得出内容再与user表进行链接。

浙公网安备 33010602011771号