一、注意事项
1.application.yml
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: com.example.demo.entity #配置实体类
2.pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3.xml #{} 和${}的区别:
1、 #{} 将传入的参数(数据)对传入的数据加一个双引号在SQL显示中 ${} 将传入的参数(数据)直接显示生成在SQL中
2、#{}可以防止SQL注入的风险(语句的拼接);但${}无法防止SQL注入。
3、${}方式一般用于传入数据库对象,例如:表名用参数传递进SQL。
4、大多数情况下还是经常使用#{},一般能用#{}的就别用${};但有些情况下必须使用${}, 例:MyBatis排序时使用ORDER BY动态参数时需要注意,得用${}而不是#{}。
4.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="mapper的接口位置">
</mapper>
二、 增删改查
<insert id="insert(与mapper层对应)" parameterType=”com.lwzy.bean.SysUser(参数类型)”>
sql语句
</insert>
<update id="updateUser(与mapper层对应)" parameterType=”com.lwzy.bean.SysUser(参数类型)”>
sql语句
</update>
<delete id="deleteUser"(与mapper层对应)>
sql语句
</delete>
<select id="接口方法名称" parameterType="int" resultType="返回值类型">
sql语句
</select>
三、数据库字段名与实体类字段名“毫无关联”
1.配置文件
<configuration>
<!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认false-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
2.起别名
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.itheima.domain.User">
select id as userId,username as userName,birthday as userBirthday,sex as userSex,address as userAddress from user
</select>
实体属性名 as 数据库表中列名
优点:查询效率高
3.使用resultMap标签
<!--id 标签:用于指定主键字段result 标签:用于指定非主键字段column 属性:用于指定数据库列名property 属性:用于指定实体类属性名称-->
<!-- 配置查询所有操作-->
<select id="findAll" resultMap="userMap">
select * from user
</select>
<!-- 建立User实体和数据库表的对应关系type属性:指定实体类的全限定类名
id属性:给定一个唯一标识,是给查询select 标签引用的。-->
<resultMap type="com.itheima.domain.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
<result column="birthday" property="userBirthday"/>
</resultMap>
优点:代码书写简洁,提高开发效率
缺点:查询效率低
四、传递多个参数时
//mapper层
UserInfo signin(@Param("account") String account,@Param("passcode") String passcode);
//xml
<select id="signin" resultType="UserInfo">
select *
from userinfo
where account=#{account} and passcode=#{passcode}
</select>
五、动态sql
MyBatis的动态sql指的是sql语句可以随着查询条件的变化而变化。
当你的查询业务比较复杂,又要灵活的查询场景时使用:
if标签:当只有if没有where时要用where1=1
<select id="selectWebsite" resultType="net.biancheng.po.Website">
select id,name,url from website
<where>
<if test="name != null">
AND name like #{name}
</if>
<if test="url!= null">
AND url like #{url}
</if>
</where>
</select>
foreach标签:与in连用( collection="list" 表名类型list,map,array数组)
collection:参数名称,根据Mapper接口的参数名确定,也可以使用@Param注解指定参数名
item:参数调用名称,通过此属性来获取集合单项的值(随便起名)
open:相当于prefix,即在循环前添加前缀
close:相当于suffix,即在循环后添加后缀
index:索引、下标(可以不写)
separator:分隔符,每次循环完成后添加此分隔符
<insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified)
VALUES
<foreach collection="list" item="it" index="index" separator =",">
(#{it.userName}, #{it.pwd}, #{it.nickName}, #{it.avatar},now(),now())
</foreach >
</insert>
<select id="findByIds" resultMap="userResultMap">
select * from user
<where>
id in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
六、MyBatis 一对一,一对多,多对多
1.一对一
private int id;
//用户ID
private int userId;
//订单数量
private String number;
//和用户表构成一对一的关系,即一个订单只能由一个用户创建
private User user;
<select id="selectOrderAndUserByOrderID" resultMap="getOrderAndUser">
select * from orders o,user u where o.user_id=u.id and o.id=#{id}
</select>
<resultMap type="com.ys.po.Orders" id="getOrderAndUser">
<!--
id:指定查询列表唯一标识,如果有多个唯一标识,则配置多个id
column:数据库对应的列
property:实体类对应的属性名
-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<!--association:用于映射关联查询单个对象的信息
property:实体类对应的属性名
javaType:实体类对应的全类名
-->
<association property="user" javaType="com.ys.po.User">
<!--
id:指定查询列表唯一标识,如果有多个唯一标识,则配置多个id
column:数据库对应的列
property:实体类对应的属性名
-->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
</association>
</resultMap>
2.一对多
private int id;
//用户姓名
private String username;
//用户性别
private String sex;
//一个用户能创建多个订单,用户和订单构成一对多的关系
public List<Orders> orders;
<select id="selectUserAndOrdersByUserId" resultMap="getUserAndOrders">
select u.*,o.id oid,o.number number from user u,orders o where u.id=o.user_id and u.id=#{id}
</select>
<resultMap type="com.ys.po.User" id="getUserAndOrders">
<!--id:指定查询列表唯一标识,如果有多个唯一标识,则配置多个id
column:数据库对应的列
property:实体类对应的属性名 -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<!--
property:实体类中定义的属性名
ofType:指定映射到集合中的全类名
-->
<collection property="orders" ofType="com.ys.po.Orders">
<id column="oid" property="id"/>
<result column="number" property="number"/>
</collection>
</resultMap>
3.多对多(使用中间表来操作)
注:所以对于多对多表,通过关系表就建立起了两张表的联系!多对多表时建立主外键后,要先删除约束表内容再
<select id="getUserByRoleId" resultMap="getUserMap">
select * from user_role ur,user u where ur.user_id=u.id and ur.role_id=#{id}
</select>
<resultMap type="com.ys.po.User" id="getUserMap">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
</resultMap>
删除主表内容