Mybatis的Jar包下載下傳:https://pan.baidu.com/s/16P-MGgn53e1EtCL6wQ9VWA 密碼:1azq
UserMapper.xml(實體類的Sql配置檔案,動态sql(标簽),sql片段):
<?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">
<!-- 配置原生Sql語句 -->
<mapper namespace="com.xxx.mybatis.mapper.UserMapper">
<!-- Sql片段 -->
<sql id="selector">
select id, username, birthday, sex, address from user
</sql>
<!-- <if>标簽, <where>标簽 -->
<!-- 根據性别和名字查詢使用者(多個where條件)。 <where>标簽可以自動去掉條件前面多餘的and (條件後面多餘的and不能去掉,是以and要寫在條件前面) -->
<select id="selectUserBySexAndUsername" parameterType="User" resultType="User">
<!-- <include refid="selector"/> --> <!-- 可以通過include标簽引入sql片段,避免書寫大量重複的sql -->
select * from user
<where>
<if test="sex != null and sex != ''"> <!-- <if>标簽。 如果輸入參數user對象的sex屬性不為null且不為'' -->
and sex = #{sex}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
</where>
</select>
<!-- <foreach>标簽 -->
<!-- 根據多個ID查詢。 id in (1,2,3) -->
<select id="selectUserByIds" resultType="User"> <!-- 可以通過包裝類屬性設定要周遊的集合(或數組)。 parameterType="QueryVo" -->
<include refid="selector"/> <!-- 可以通過include标簽引入sql片段,避免書寫大量重複的sql -->
<where>
<!-- id in (1,2,3) -->
<!-- collection="ids" 可以周遊包裝類的ids屬性(List類型或數組類型)。
collection="list" 表示周遊的是集合。
collection="array" 表示周遊的是數組 -->
<foreach collection="list" item="id" separator="," open="id in (" close=")">
#{id}
</foreach>
</where>
</select>
<!-- <set>标簽 -->
<!-- 修改 -->
<update id="updateCustomerById" parameterType="Customer">
update customer
<set>
<if test="cust_name != null">
cust_name = #{cust_name},
</if>
<if test="cust_linkman != null">
cust_linkman = #{cust_linkman},
</if> <!-- set标簽會自動将最後多餘的逗号","去除 -->
</set>
<where>
cust_id = #{cust_id}
</where>
</update>
</mapper>
QueryVo.java(包裝類,封裝sql占位符的輸入參數):
package com.xxx.mybatis.pojo;
import java.io.Serializable;
import java.util.List;
// pojo包裝對象
public class QueryVo implements Serializable {
private static final long serialVersionUID = 1L;
//private User user; // 簡單pojo對象作為包裝對象的屬性
List<Integer> ids; //動态sql;在mapper.xml檔案中可以通過foreach标簽周遊生成動态sql。
//Integer[] ids;
public List<Integer> getIdsList() {
return ids;
}
public void setIdsList(List<Integer> ids) {
this.ids = ids;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Test.java(測試類):
package com.xxx.mybatis.junit;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.xxx.mybatis.mapper.UserMapper;
import com.xxx.mybatis.pojo.User;
public class Test {
//根據性别和名字查詢使用者
@Test
public void testfindUserBySexAndUsername() throws Exception {
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
//---------------------------------------------------------------------
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex("1");
//user.setUsername("張小明");
List<User> users = userMapper.selectUserBySexAndUsername(user);
for (User user2 : users) {
System.out.println(user2);
}
}
//根據多個ID查詢
@Test
public void testfindUserIDs() throws Exception {
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
//---------------------------------------------------------------------
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//Integer[] ids = new Integer[]{16,24,22};
List<Integer> ids = new ArrayList<Integer>();
ids.add(16);
ids.add(22);
ids.add(24);
List<User> users = userMapper.selectUserByIds(ids); //ids可以是List也可以是數組
/* QueryVo vo = new QueryVo(); //包裝類
vo.setIdsList(ids);
List<User> users = userMapper.selectUserByIds(vo); */
for (User user : users) {
System.out.println(user);
}
}
}
UserMapper.java(遵循四個原則的Dao層接口):
package com.xxx.mybatis.mapper;
import java.util.List;
import com.xxx.mybatis.pojo.QueryVo;
import com.xxx.mybatis.pojo.User;
//其實就是Dao層接口
public interface UserMapper {
//遵循四個原則:
//UserMapper.xml中配置的命名空間要與該接口的全類名保持一緻(com.xxx.mybatis.mapper.UserMapper)
//接口中的方法名 == UserMapper.xml中配置的sql語句的id名
//傳回值類型 與 UserMapper.xml檔案中配置的傳回值類型(resultType)要一緻
//方法的輸入參數類型 與UserMapper.xml中配置的輸入參數的類型(parameterType)要一緻
//根據性别和名字查詢使用者
public List<User> selectUserBySexAndUsername(User user);
//根據多個id查詢使用者資訊
//public List<User> selectUserByIds(Integer[] ids); //參數是數組
public List<User> selectUserByIds(List<Integer> ids); //參數是List
//public List<User> selectUserByIds(QueryVo vo); //參數是包裝類
}