国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

mybatis基本用法

2019-11-08 03:13:11
字體:
來源:轉載
供稿:網友

xml

可以看作是接口實現類

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace命名空間,等于mapper接口地址--><mapper namespace="com.mybatis.mapper.UserMapper"> <!-- 定義sql片段,提高重用性 id:被引用的唯一標識 經驗:在sql片段中不要包括 where --> <sql id="query_user_where"> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=''"> and user.sex = #{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=''"> and user.username LIKE concat('%', #{name}, '%') </if> <if test="ids!=null"> <!-- 使用 foreach遍歷傳入ids collection:指定輸入 對象中集合屬性 item:每個遍歷生成對象中 open:開始遍歷時拼接的串 close:結束遍歷時拼接的串 separator:遍歷的兩個對象中需要拼接的串 --> <!-- 使用實現下邊的sql拼接: AND (id=1 OR id=10 OR id=16) --> <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"> <!-- 每個遍歷需要拼接的串 --> id=#{user_id} </foreach> <!-- 實現 “ and id IN(1,10,16)”拼接 --> <!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=","> 每個遍歷需要拼接的串 #{user_id} </foreach> --> </if> </if> </sql> <!-- 映射關系 type:resultMap最終映射的java對象類型,可以使用別名 id:唯一標識 --> <resultMap type="user" id="userResultMap"> <!-- id:查詢結果集中唯一標識 column:查詢出來的列名 PRoperty:type指定的pojo類型中的屬性名 --> <id column="id_" property="id"/> <!-- result:對普通名映射定義 --> <result column="username_" property="username"/> </resultMap> <!-- 用戶信息綜合查詢 #{userCustom.sex}:取出pojo包裝對象中性別值 ${userCustom.username}:取出pojo包裝對象中用戶名稱 --> <select id="findUserList" parameterType="com.mybatis.po.UserQueryVo" resultType="cn.itcast.mybatis.po.UserCustom"> SELECT * FROM USER <!-- where可以自動去掉條件中的第一個and --> <where> <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前邊加namespace --> <include refid="query_user_where"></include> <!-- 在這里還要引用其它的sql片段 所以引入的sql片段中不要加where --> </where> </select> <!-- 用戶信息綜合查詢總數 parameterType:輸入類型 resultType:輸出類型 --> <select id="findUserCount" parameterType="com.mybatis.vo.UserQueryVo" resultType="int"> SELECT count(*) FROM USER <where> <include refid="query_user_where"></include> </where> </select> <!-- 通過id查詢用戶表的記錄 --> #{}: 占位符 #{id}:其中的id表示接收輸入 的參數,參數名稱就是id,如果輸入 參數是簡單類型,#{}中的參數名可以任意,可以value或其它名稱 resultType:輸出結果映射的java對象類型,select指定resultType表示將單條記錄映射成的java對象 --> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM USER WHERE id=#{value} </select> <!-- 使用resultMap進行輸出映射 resultMap:指定定義的resultMap的id,如果這個resultMap在其它的mapper文件,前邊需要加namespace --> <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap"> SELECT id id_,username username_ FROM USER WHERE id=#{value} </select> <!-- 根據用戶名稱模糊查詢用戶信息,可能返回多條 ${}:表示拼接sql串,將接收到參數的內容不加任何修飾拼接在sql中,可能會引起 sql注入,一般應用在order by; ${value}:接收輸入 參數的內容,如果傳入類型是簡單類型,${}中只能使用value; #{xxx},使用的是PreparedStatement,有類型轉換,所以比較安全; ${xxx},使用字符串拼接,可以SQL注入; like查詢不小心會有漏動,正確寫法如下: MySQL: select * from t_user where name like concat('%', #{name}, '%') Oracle: select * from t_user where name like '%' || #{name} || '%' SQLServer: select * from t_user where name like '%' + #{name} + '%' --> <select id="findUserByName" parameterType="java.lang.String" resultType="com.mybatis.po.User"> <!-- SELECT * FROM USER WHERE username LIKE '%${value}%' --> SELECT * FROM USER WHERE username LIKE concat('%', #{name}, '%') </select> <!-- 添加用戶 --> <insert id="insertUser" parameterType="com.mybatis.po.User"> <!-- 將插入數據的主鍵返回,返回到user對象中 SELECT LAST_INSERT_ID():得到剛insert進去記錄的主鍵值,只適用與自增主鍵 keyProperty:將查詢到主鍵值設置到parameterType指定的對象的哪個屬性 order:SELECT LAST_INSERT_ID()執行順序,相對于insert語句來說它的執行順序 resultType:指定SELECT LAST_INSERT_ID()的結果類型 --> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> <!-- #{}中指定pojo的屬性名,接收到pojo對象的屬性值,mybatis通過OGNL獲取對象的屬性值 --> insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) <!-- 使用mysql的uuid()生成主鍵 執行過程: 首先通過uuid()得到主鍵,將主鍵設置到user對象的id屬性中 其次在insert執行時,從user對象中取出id屬性值 --> <!-- <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> SELECT uuid() </selectKey> insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address}) --> </insert> <!-- 刪除用戶 --> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete> <!-- 更新用戶 --> <update id="updateUser" parameterType="com.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update></mapper>

mapper

接口 UserMapper.java

package com.mybatis.mapper;import java.util.List;import com.mybatis.po.User;import com.mybatis.po.UserCustom;import com.mybatis.vo.UserQueryVo;public interface UserMapper { //用戶信息綜合查詢 public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception; //用戶信息綜合查詢總數 public int findUserCount(UserQueryVo userQueryVo) throws Exception; //根據id查詢用戶信息 public User findUserById(int id) throws Exception; //根據id查詢用戶信息,使用resultMap輸出 public User findUserByIdResultMap(int id) throws Exception; //根據用戶名列查詢用戶列表 public List<User> findUserByName(String name)throws Exception; //插入用戶 public void insertUser(User user)throws Exception; //刪除用戶 public void deleteUser(int id)throws Exception;}

vo

UserQueryVo.java

package com.mybatis.vo;import java.util.List;public class UserQueryVo { //傳入多個id private List<Integer> ids; //用戶查詢條件 private UserCustom userCustom; public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; }}

po

UserCustom.java

package com.mybatis.po;public class UserCustom extends User{ //可以擴展用戶的信息}

User.java

package com.mybatis.po;import java.util.Date;public class User { //屬性名和數據庫表的字段對應 private int id; private String username;// 用戶姓名 private String sex;// 性別 private Date birthday;// 生日 private String address;// 地址 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邢台市| 凯里市| 潮州市| 灵台县| 孝义市| 宣威市| 游戏| 紫阳县| 临安市| 光泽县| 奉新县| 响水县| 措勤县| 资溪县| 凤凰县| 阿瓦提县| 甘孜| 洪泽县| 东港市| 靖江市| 枞阳县| 博野县| 织金县| 阿勒泰市| 贵德县| 苗栗市| 白水县| 嵊州市| 延庆县| 永登县| 棋牌| 怀宁县| 惠水县| 兴仁县| 安国市| 酒泉市| 北宁市| 宁陵县| 新营市| 山丹县| 建宁县|