1、寫入數據并獲取自增ID
XML配置:
<!-- 寫入數據獲取自增ID --><insert id="insertLog" parameterType="com.mamaguwen.entity.sys_loginlog" useGeneratedKeys="true" keyProperty="logid">insert into sys_loginlog (UserName) values (#{username}) </insert>
測試代碼:
@Testpublic void insertLog() {sys_loginlog model = new sys_loginlog();model.setIslogin(true);model.setLoginip("127.0.0.1");model.setLogintime(new Date());model.setUsername("rhythmk");int total = loginlog.insertLog(model);System.out.println("影響數據條:" + total);System.out.println("ID:" + model.getLogid());/** 影響數據條:1 ID:4 */}
2、更新數據
<!-- 更新數據 --><update id="updateLog" parameterType="com.mamaguwen.entity.sys_loginlog">update sys_loginlog set username=#{username}where LogId=#{logid}</update>
/** 更新數據*/@Testpublic void updateLog() {sys_loginlog record = new sys_loginlog();record.setLogid(4L);record.setUsername("wangkun");int total = loginlog.updateLog(record);System.out.println("影響數據條:" + total);}
3、返回單個字符串對象:
<!-- -返回單字段內容 --><select id="selectStringByKey" resultType="String" >select UserName from sys_loginlogwhere LogId = #{logid}</select>
/** 返回當個簡單對象*/@Testpublic void selectStringByKey() {String record = loginlog.selectStringByKey(4);System.out.println("返回的字符串:" + record);}
4、返回List對象
<select id="selectLogList" resultType="com.mamaguwen.entity.sys_loginlog">select * from sys_loginlog</select>
/** 獲取所有用戶日志*/@Testpublic void selectLogList() {List<sys_loginlog> list = loginlog.selectLogList();for (sys_loginlog log : list) {System.out.println(log.getUsername());}}
5、返回List<String> 對象
<select id="selectUserNameList" resultType="String">select UserName from sys_loginlog</select>
/** 獲取所有用戶名*/@Testpublic void selectUserNameList() {List<String> list = loginlog.selectUserNameList();for (String str : list) {System.out.println(str);}}
6、傳入單個參數
<select id="selectLogByKey" resultType="com.mamaguwen.entity.sys_loginlog">select * from sys_loginlog Where LogId=#{logid}</select>
/** 根據主鍵獲取日志*/@Testpublic void selectLogByKey() {sys_loginlog model = loginlog.selectLogByKey(5);String str = String.format("id:%d,username:%s", model.getLogid(),model.getUsername());System.out.println(str);}
7、執行存儲過程:
<!-- 執行存儲過程 --><select id="callProc" resultType="String" ><!-- drop procedure if exists ShowString;CREATE PROCEDURE ShowString(Str VARCHAR(30))BEGINselect Str as Item;END;CALL ShowString('rhythmk')-->call ShowString (#{str})</select>
/** 執行存儲過程*/@Testpublic void callProc() {String str = loginlog.callProc("rhytmk");System.out.println(str);}
8、批量寫入數據
<!-- 批量執行SQL --><!-- 生成SQL:insert into sys_loginlog (username) values ('a'),('b')--><insert id="insertBatch">insert into sys_loginlog (username) values<foreach collection="list" item="item" index="index" separator=",">(#{item.username})</foreach></insert>
/** 批量寫入*/@Testpublic void insertBatch() {List<sys_loginlog> list = new ArrayList<sys_loginlog>();for (int i = 0, j = 10; i < j; i++) {sys_loginlog log = new sys_loginlog();log.setUsername(String.format("wangkun%s", i));list.add(log);}int total = loginlog.insertBatch(list);System.out.println("生成數據條:" + total);}
9、將字符串當參數出入進去
<select id="selectLogByMap" parameterType="Map" resultType="com.mamaguwen.entity.sys_loginlog">select * from sys_loginlogwhere (username=#{username1} or username=#{username2} )</select>
/** 通過Map傳入參數*/@Testpublic void selectLogByMap(){Map<String, String> map=new HashMap<String,String>();map.put("username1", "rhythmk");map.put("username2", "wangkun");List<sys_loginlog> list= loginlog.selectLogByMap(map);for(sys_loginlog model:list){String info= String.format("id%d,username%s", model.getLogid(),model.getUsername());System.out.println(info);}}
?10、#{}與${}的區別
? ? ?假如數據庫 sys_loginlog表中有username=a,b兩條數據。此時按下面配置文件,我傳入'a','b' ?則無法獲取數據。?
<select id="selectLogByUserName" parameterType="Map" resultType="com.mamaguwen.entity.sys_loginlog">select * from sys_loginlogwhere username in ( #{username} )</select>
現修改where條件,換成${},那么傳入的參數講直接體會SQL中對應的文本 :
select * from sys_loginlogwhere username in ( ${username} )
通過執行上面語句 生成的SQL為 :
select * from sys_loginlog where username in ('a','b')
備注:
? ?表結構:
CREATE TABLE `sys_loginlog` (`LogId` bigint(20) NOT NULL AUTO_INCREMENT,`UserName` varchar(64) COLLATE utf8_bin DEFAULT NULL,`Pwd` varchar(32) COLLATE utf8_bin DEFAULT NULL,`IsLogin` bit(1) DEFAULT NULL,`LoginIp` varchar(64) COLLATE utf8_bin DEFAULT NULL,`LoginTime` datetime DEFAULT NULL,PRIMARY KEY (`LogId`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
?Mapp數據操作接口:
public interface sys_loginlogMapper {/** 寫入日志并返回自增的ID* */int insertLog (sys_loginlog record);/* 更新數據* */int updateLog(sys_loginlog record);/* * 返回當個簡單對象* */String selectStringByKey(@Param("logid") int logId );/** 獲取所有用戶日志* */List<sys_loginlog> selectLogList();/** 獲取所有用戶名* */List<String> selectUserNameList();/** 根據主鍵獲取日志* */sys_loginlog selectLogByKey(@Param("logid") int logid);/** 執行存儲過程* */String callProc(@Param("str") String str);/** 批量寫入* */int insertBatch(List<sys_loginlog> list);/** 通過Map傳入參數* */List<sys_loginlog> selectLogByMap(Map<String, String> map);}
?測試用例代碼:


package com.mamaguwen.dao.test;import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;import javax.management.loading.PrivateMLet;import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.ibatis.annotations.Param; import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.mamaguwen.dao.sys_loginlogMapper; import com.mamaguwen.entity.sys_loginlog;@RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring.xml","classpath:spring-mybatis.xml" }) public class TestSysloginlogMapper {private static final Logger logger = Logger.getLogger(Test_SysUser.class);private sys_loginlogMapper loginlog;public sys_loginlogMapper getLoginlog() {return loginlog;}@Autowiredpublic void setLoginlog(sys_loginlogMapper loginlog) {this.loginlog = loginlog;}/** 寫入日志并返回自增的ID*/@Testpublic void insertLog() {sys_loginlog model = new sys_loginlog();model.setIslogin(true);model.setLoginip("127.0.0.1");model.setLogintime(new Date());model.setUsername("rhythmk");int total = loginlog.insertLog(model);System.out.println("影響數據條:" + total);System.out.println("ID:" + model.getLogid());/** 影響數據條:1 ID:4 */}/** 更新數據*/@Testpublic void updateLog() {sys_loginlog record = new sys_loginlog();record.setLogid(4L);record.setUsername("wangkun");int total = loginlog.updateLog(record);System.out.println("影響數據條:" + total);}/** 返回當個簡單對象*/@Testpublic void selectStringByKey() {String record = loginlog.selectStringByKey(4);System.out.println("返回的字符串:" + record);}/** 獲取所有用戶日志*/@Testpublic void selectLogList() {List<sys_loginlog> list = loginlog.selectLogList();for (sys_loginlog log : list) {System.out.println(log.getUsername());}}/** 獲取所有用戶名*/@Testpublic void selectUserNameList() {List<String> list = loginlog.selectUserNameList();for (String str : list) {System.out.println(str);}}/** 根據主鍵獲取日志*/@Testpublic void selectLogByKey() {sys_loginlog model = loginlog.selectLogByKey(5);String str = String.format("id:%d,username:%s", model.getLogid(),model.getUsername());System.out.println(str);}/** 執行存儲過程*/@Testpublic void callProc() {String str = loginlog.callProc("rhytmk");System.out.println(str);}/** 批量寫入*/@Testpublic void insertBatch() {List<sys_loginlog> list = new ArrayList<sys_loginlog>();for (int i = 0, j = 10; i < j; i++) {sys_loginlog log = new sys_loginlog();log.setUsername(String.format("wangkun%s", i));list.add(log);}int total = loginlog.insertBatch(list);System.out.println("生成數據條:" + total);}/** 通過Map傳入參數*/@Testpublic void selectLogByMap(){Map<String, String> map=new HashMap<String,String>();map.put("username1", "rhythmk");map.put("username2", "wangkun");List<sys_loginlog> list= loginlog.selectLogByMap(map);for(sys_loginlog model:list){String info= String.format("id%d,username%s", model.getLogid(),model.getUsername());System.out.println(info);}} }
?