1. 程式人生 > >Mybatis-Mapper對映sql片段例項

Mybatis-Mapper對映sql片段例項

Mapper.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.xdw.mybatis.mapper.Mapper">

<resultMap type="user" id="userResultMap">
<id  column="id_" property="id"/>
<result column="username_" property="username"/>
</resultMap>

<select id="findUserByIdResultMap" parameterType="int"  resultMap="userResultMap">
	select id id_,username username_ from user where id=#{id}
</select>

<select id="findUserById" parameterType="int" resultType="user">
  select * from user where id=#{id}
</select>

<select id="findUserByName" parameterType="String" resultType="com.xdw.mybatis.pojo.User">
  select * from user where username like '%${value}%'
</select>

<sql id="findUser">
 <if test="custom!=null">
 <if test="custom.sex!=null and custom.sex!='' "> sex = #{custom.sex} </if>
 <if test="custom.username!=null and custom.username!='' ">and 
 username like '%${custom.username}%' </if>
 <if test="ids!=null">
<foreach collection="ids" item="item_id" open="and id in("  
close=")"  separator=",">
  	#{item_id}
</foreach>
  			</if>
  		</if>
</sql>

<!-- ${}:表示拼接sql串,將接收到引數的內容不加任何修飾拼接在sql中   -->
<select id="findUserList" parameterType="com.xdw.mybatis.queryVo.UserQueryVo" resultType="com.xdw.mybatis.pojo.UserCustom">
  select * from user 
  <where>
  		<include refid="findUser"></include>
  </where>
</select>

<insert id="insertUser" parameterType="com.xdw.mybatis.pojo.User">
<selectKey keyProperty="id" order="AFTER" resultType="integer">
   select LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>

<delete id="deleteUser" parameterType="integer">
delete from user where id=#{value}
</delete>

<update id="updateUser" parameterType="com.xdw.mybatis.pojo.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update>

</mapper>

MapperTest.java

public class MapperTest {
	
	private SqlSessionFactory ssf;

	@Before
	public void setUp() throws IOException {
		String resourse = "sqlMapConfig.xml";

		InputStream is = Resources.getResourceAsStream(resourse);

		ssf = new SqlSessionFactoryBuilder().build(is);
	}

	@Test
	public void FindUserById() {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		User user = mapper.findUserById(31);
		
		ss.close();

		System.out.println(user);
	}
	
	@Test
	public void FindUserByName() {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		List<User> list = mapper.findUserByName("m");
		
		ss.close();
		
		System.out.println(list);
	}
	
	@Test
	public void findUserByIdResultMap() throws Exception {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		User user= mapper.findUserByIdResultMap(33);
		
		ss.close();
		
		System.out.println(user);
	}
	
	@Test
	public void findUserList() throws Exception {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		UserQueryVo queryVo = new UserQueryVo();
		UserCustom custom  = new UserCustom();
		List<Integer> list_id = new ArrayList<Integer>();
		
		custom.setSex("1");
		custom.setUsername("張");
		
		list_id.add(16);
		list_id.add(22);
		list_id.add(24);
		
		queryVo.setIds(list_id);
		queryVo.setCustom(custom);
		
	    List<UserCustom> list =  mapper.findUserList(queryVo);
		
		ss.close();
		
		System.out.println(list);
		
	}
	
	
	@Test
	public void insertUser() {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		User user = new User();
		user.setAddress("guangzhou");
		user.setBirthday(new Date());
		user.setSex("1");
		user.setUsername("m");
		
		mapper.insertUser(user);
		
		ss.commit();
		
		ss.close();
	}
	
	@Test
	public void deleteUser() {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		mapper.deleteUser(29);
		
		ss.commit();
		
		ss.close();
	}
	
	@Test
	public void updateUser() {
		
		SqlSession ss = ssf.openSession();
		
		Mapper mapper =ss.getMapper(Mapper.class);
		
		User user = new User();
		user.setId(33);
		user.setAddress("changsha");
		user.setBirthday(new Date());
		user.setSex("2");
		user.setUsername("aaaaa");
		
		mapper.updateUser(user);
		
		ss.commit();
		
		ss.close();
	}

}

UserQueryVo.java

public class UserQueryVo {

	private UserCustom custom;
	private List<Integer> ids;

	public UserQueryVo(UserCustom custom) {
		super();
		this.custom = custom;
	}

	public UserQueryVo() {
	}

	public UserCustom getCustom() {
		return custom;
	}

	public void setCustom(UserCustom custom) {
		this.custom = custom;
	}

	public List<Integer> getIds() {
		return ids;
	}

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}

}

UserCustom類繼承User類,省略。

    private int id;
    private String username;// 使用者姓名
    private String sex;// 性別
    private Date birthday;// 生日
    private String address;// 地址