手機網(wǎng)站內(nèi)容模塊如何進(jìn)行網(wǎng)站宣傳推廣
目錄
一、定義接口、實體類、創(chuàng)建XML文件實現(xiàn)接口)
二、MyBatis的增刪改查?
🍅1、MyBatis傳遞參數(shù)查詢
????????🎈寫法一
?????????🎈寫法二
? ? ? ? 🎈兩種方式的區(qū)別
🍅2、刪除操作
🍅3、根據(jù)id修改用戶名
🍅4、添加用戶操作
? ? ? ? 🎈返回受影響的行數(shù)
? ? ? ? 🎈返回自增id
🍅5、like查詢
🍅6、多表查詢
三、注意
🍅1、mybatisx插件報錯
🍅2、數(shù)據(jù)回滾
🍅 3、查詢某字段結(jié)果為null時
一、定義接口、實體類、創(chuàng)建XML文件實現(xiàn)接口)
注意包名:
實體類
package com.example.demo.model;import lombok.Data;import java.time.LocalDateTime;@Data
public class UserInfo {private int id;private String username;private String password;private String photo;private LocalDateTime updatatime;private LocalDateTime createtime;private int state;}
接口
package com.example.demo.dao;import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper //數(shù)據(jù)持久層的標(biāo)志
public interface UserMapper {List<UserInfo> getAll();
}
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.example.demo.dao.UserMapper">
<!-- id是UserMapper的方法名--><select id="getAll" resultType="com.example.demo.model.UserInfo">
-- 不寫分號select * from userinfo</select>
</mapper>
二、MyBatis的增刪改查?
🍅1、MyBatis傳遞參數(shù)查詢
????????🎈寫法一
@Param("id")與${id}相互匹配 (及時執(zhí)行)
使用id去查詢某條數(shù)據(jù)(傳參數(shù))
UserInfo getUserById(@Param("id") Integer uid);
-- 這里應(yīng)該寫${id},而不是uid
<select id="getUserById" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=${id}</select>
測試類:?
@Testvoid getUserById() {UserInfo userInfo = userMapper.getUserById(1);System.out.println(userInfo.toString());}
?????????🎈寫法二
@Param("id")與#{id}相互匹配 (預(yù)執(zhí)行)
<select id="getUserById" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=#{id}</select>
? ? ? ? 🎈兩種方式的區(qū)別
1.${}是直接替換;而#{}是預(yù)執(zhí)行
2.使用${}是不安全的,存在SQL注入;而#{}是安全的,不存在SQL注入
3.${}使用場景:當(dāng)業(yè)務(wù)需要傳遞SQL語句時,只能使用${},不能使用#{}。
SQL注入:將SQL代碼插入或添加到應(yīng)用(用戶)的輸入?yún)?shù)中的攻擊,之后再將這些參數(shù)傳遞給后臺的sql服務(wù)器加以解析和執(zhí)行。
🍅2、刪除操作
UserInfo getUserById(@Param("id") Integer uid);
<!-- delete操作不需要設(shè)置返回類型--><delete id="delById">delete from userinfo where id=#{id}</delete>
?測試類
@Transactional //數(shù)據(jù)回滾:使用該注解,會執(zhí)行下面操作,但是不會真的操作數(shù)據(jù)庫中的內(nèi)容@Testvoid testGetUserById() {int id = 1;UserInfo result = userMapper.getUserById(id);System.out.println("受影響的行數(shù):"+result);}
🍅3、根據(jù)id修改用戶名
// 根據(jù)id修改用戶名
// 返回受影響行數(shù)int update(UserInfo userInfo);
<!-- 默認(rèn)返回受影響的行數(shù),不需要設(shè)置resultType--><update id="update" >update userinfo set username=#{username} where id=#{id}</update>
@Transactional@Testvoid update() {UserInfo userInfo = new UserInfo();userInfo.setId(1);userInfo.setUsername("管理員");int result = userMapper.update(userInfo);System.out.println("受影響行數(shù)"+ result);}
?運行結(jié)果:
🍅4、添加用戶操作
? ? ? ? 🎈返回受影響的行數(shù)
//返回受影響字段
int add(UserInfo userInfo);
<insert id="add">insert into userinfo(username,password,photo)
values (#{username},#{password},#{photo})</insert>
//測試類
@Testvoid add() {UserInfo userInfo = new UserInfo();userInfo.setUsername("張三");userInfo.setPassword("11111");userInfo.setPhoto("/image/default.png");int result = userMapper.add(userInfo);System.out.println("受影響的行數(shù):"+ result);}
?運行結(jié)果:
? ? ? ? 🎈返回自增id
int insert(UserInfo userInfo);
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into userinfo(username,password,photo) values (#{username},#{password},#{photo})</insert>
//測試類
@Testvoid insert() {UserInfo userInfo = new UserInfo();userInfo.setUsername("李四");userInfo.setPassword("111111");userInfo.setPhoto("");int result = userMapper.insert(userInfo);System.out.println("受影響的行數(shù):" + result +"| id:"+ userInfo.getId());}
?運行結(jié)果:
?對應(yīng)關(guān)系:
🍅5、like查詢
List<UserInfo> getListByLike(@Param("username")String username);
<select id="getListByLike" resultType="com.example.demo.model.UserInfo">select * from userinfo where username like concat('%',#{username},'%')</select>
@Testvoid getListByLike() {String username = "三";List<UserInfo> list = userMapper.getListByLike(username);System.out.println(list);}
運行結(jié)果:
🍅6、多表查詢
使用注解將sql語句和接口連接起來。
之前是,寫一個接口就要寫一個對應(yīng)的xml文件編寫sql語句。現(xiàn)在可以不使用這種方法
可以直接在接口中通過注解編寫查詢語句:
@Mapper
public interface ArticleMapper{@Select("select a.*,u.username from articleinfo a" +"left join userinfo u on a.uid=u.id")List<Articleinfo> getAll();
}
測試類:
class ArticleMapperTest {@Autowiredprivate ArticleMapper articleMapper;@Testvoid getAll() {List<Articleinfo> list = articleMapper.getAll();System.out.println(list);}
}
?
三、注意
🍅1、mybatisx插件報錯
?以上并非系統(tǒng)報錯,而是插件在報錯,告訴我們沒有設(shè)置返回的技術(shù)類型,高版本的idea遇到這種情況運行是不會報錯的。
🍅2、數(shù)據(jù)回滾
?@Transactional ? //數(shù)據(jù)回滾:使用該注解,會執(zhí)行下面操作,但是不會真的操作數(shù)據(jù)庫中的內(nèi)容
🍅 3、查詢某字段結(jié)果為null時
原因是:實體類中的屬性和數(shù)據(jù)庫表中的字段名不一致。
? ? ? ?解決方案:
????????1.將實體類中的屬性和表中的字段名保持一致(最簡單的解決方案)
? ? ? ? 2.使用sql語句中的as進(jìn)行列名(字段名)重命名,讓列名(字段名)等于屬性名
<!-- 加入要查詢的字段名應(yīng)該是name,對name進(jìn)行重命名 --> <selet id="getUserById" resultType="com.example.demo.model.UserInfo">select username as name from userinfo where id=#{id}c</select>
? ? ? ? 3.定義一個resultMap,將屬性名和字段名進(jìn)行手動映射
? ? ? ? resultMap放入mapper.xml的<mapper></mapper>中?
<resultMap id="UserMapper" type="com.example.demo.model.UserInfo"><id column="id" property="id"></id><result column="username" property="username"></result><result column="password" property="password"></result></resultMap> <select id="getUserById" resultMap="BaseMap">select * from userinfo where id=#{id}</select>
以下是對應(yīng)關(guān)系:盡量把全部屬性映射上,否則多表查詢時可能會報錯。
上圖有個錯誤!!!??colum是表里面的字段名,property是實體類里的屬性,寫反了。