User.java
package com.qfedu.entities; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private static final long serialVersionUID=1L; private int id; private String username; private Date birthday; private String sex; private String address; public User(int id, String username, Date birthday, String sex, String address) { this.id = id; this.username = username; this.birthday = birthday; this.sex = sex; this.address = address; } public User() { } 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } }
User.xml
<mapper namespace="user">
mapper>
在User.xml配置文件中配置
在插入操作完成之前或之后,可以配置
LAST_INSERT_ID:该函数是mysql的函数,获取自增主键的ID,它必须配合insert语句一起使用
如果单纯的插入,不需要返回主键的值,
<mapper namespace="user"> <insert id="addUser" parameterType="com.qfedu.entities.User"> <selectKey keyProperty="id" resultType="int" order="AFTER"> SELECT LAST_INSERT_ID() selectKey> insert into user(username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address}) insert> mapper>
测试类:直接执行配置的statement
//4.执行 User user = new User(); user.setUsername("Jack"); user.setBirthday(new Date()); sqlSession.insert("user.addUser",user); sqlSession.commit();
在映射文件文件中使用
<delete id="deleteUser" parameterType="int"> delete from user where id=#{id} delete>
测试代码如下,直接执行配置的statement,可以查看结果。
SqlSession sqlSession = sqlSessionFactory.openSession(); //执行 sqlSession.delete("user.delUser",1); sqlSession.commit();
在映射文件中使用
<update id="updateUser" parameterType="com.qfedu.entities.User"> update user set username = #{username}, sex = #{sex}, birthday = #{birthday}, address = #{address} where id = #{id} update>
测试:
//4.执行 User user = new User(); //构建user参数,没有赋值的属性采取默认值 user.setId(33); user.setUsername("陈"); user.setAddress("青岛"); sqlSession.update("user.updateUser",user); sqlSession.commit();
在映射文件中使用标签配置查询的statement。
注意:
{} 相当于占位符
{id} 其中的id可以表示输入参数的名称,如果是简单类型,名称可以随意
${} 表示拼接slq语句
{value} 其中的value可以表示输入参数的名称,如果是简单类型,参数名必须为value
<select id="getUserById" parameterType="int" resultType="com.qfedu.entities.User"> select * from user where id = #{id} select> <select id="findByUsername" parameterType="java.lang.String" resultType="com.qfedu.entities.User"> select * from user where username like '%${value}%' select>
执行
User user = sqlSession.selectOne("user.getUserById",22); System.out.println(user); List<User> list = sqlSession.selectList("user.findByUsername","陈"); for(User user :list){ System.out.println(user); } sqlSession.commit();