MyBatis插入对象后返回获得自增的主键
很多情况下,我们的主键都是自增的,我们可能在插入后马上就获取到刚刚插入的数据的主键,下面就来记录一下:
插入记录在MyBatis中的写法:
<insert id="saveXxx" parameterType="com.wjy329.entity.xxx"> <selectKey resultType="Integer" order="AFTER" keyProperty="id"> SELECT LAST_INSERT_ID() </selectKey> insert into tableName ( `line2`, `line3` ) values ( #{line2}, #{line3} ) </insert>
关键在于
<selectKey resultType="Integer" order="AFTER" keyProperty="id"> SELECT LAST_INSERT_ID() </selectKey>
你以为仅仅获取到这个就行了?那是不可能的,直接获取返回的int,你会发现是一个固定的数字,它代表刚才插入数据影响的行。
在执行完插入操作后,我们只需在后面直接用getId()方法就行,例如:
studentDao.saveStudent(student); Integer id = student.getId;
wAdmin-基于Spring Boot+MyBatis+Layui的后台管理系统
从之前的公司那里抽离出来的一套后台管理模板,我觉得还是很方便使用的,而且使用Layui,也很美观。
1、登陆界面
2、主界面
3、用户管理
4、角色管理
5、菜单配置
Github: https://github.com/wjy329/wAdmin
Mybatis 多参数查询及where使用
背景:多维度查询,有可能都为空,也可以不为空。
<select id="queryBySomeParam" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from tablename <where> <if test="paramA !=null and paramA.length()>0" > paramA=#{paramA,jdbcType=VARCHAR} </if> <if test="paramB !=null and paramB.length()>0"> and paramB=#{paramB,jdbcType=VARCHAR} </if> <if ...> ... </if> </where> order by id desc </select>
where会根据前面的参数自动判断and的省略,如果paramA为空,则自动省略and,即,执行数据库语句为:
select * from table where paramB = xxx
多参数传递,dao中需注入参数,即:
List<Entity> queryBySomeParam(@Param(value = "paramA") String paramA, @Param(value = "paramB")String paramB ...);
Mybatis 大于等于小于等于写法
昨天在mybatis中需要写到小于等于开始直接使用了<=。。。然后代码中认为它是一个<>。。。后来查了才知道在mybtis中应该这样写;
符号 | 转义字符 |
< | < |
<= | <= |
> | > |
>= | >= |
& | & |
' | ' |
" | " |
例如:
<!-- 查找满足条件的最近一条记录 --> <select id="getMailByTypeAndSub" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from monitor_mail where type = #{type,jdbcType=VARCHAR} and subject = #{subject,jdbcType=VARCHAR} and create_time <= NOW() order by create_time desc limit 1 </select>
Mybatis 多表查询返回指定字段
有时候我们需要返回多张表的指定字段,可以使用resultType='"java.util.Map"(例子随便写的,只为演示)
<select id="queryAll" resultType="java.util.Map"> select t1.studentName ,t2.className,t2.classNum,t3.id,t3.teacherName from t_student t1,t_class t2,t_teacher t3 where t1.id = t2.cid AND t1.id = t3.tid order by t3.id desc </select>
这样就会返回[studentName:xxx,className:xxx,classNum:xxx,id:xxx,teacherName:xxx]
Mybatis 表单封装成对象提交
当前端表单有很多参数需要传入的时候,我们一个个的传入显得就很多了,这样我们就需要将传入的参数封装成一个对象。
对象的构建也很简单,就是前端有哪些参数就定义哪些,当前端有复选框的时候,我们将其定义为List类型或者Array来接受。
下面例子只是为了记录。。。参数并不多,就那么个意思。
1、表单封装实体
import java.util.List; /** * @author wjy329 * @date 2018/6/27 */ public class UserForm { /** * 用户名 * @date 2018/6/27 */ private String userName; /** * 密码 * @date 2018/6/27 */ private String passWord; /** * 作业 * @date 2018/6/27 */ private List<String> job; ... 省略get、set方法 ... }
2、实体类(与数据库对应)
/** * @author wjy329 * @date 2018/6/27 */ public class UserEntity { /** * 用户名 * @date 2018/6/27 */ private String userName; /** * 密码 * @date 2018/6/27 */ private String passWord; /** * 作业,这里不是list * @date 2018/6/27 */ private String job; ... 省略get、set方法 ... }
3、UserDao.java
public interface UserDao { /** * @return 返回结果集 * @date */ List<UserEntity> getValues(@Param("main") UserEntity userEntity, @Param("form") UserForm userForm);
4、UserDao.xml
<select id="getValues" parameterType="com.wjy329.entity.UserEntity" resultMap="BaseResultMap"> select * from user where username=#{main.username} and job in <foreach collection="form.job" index="index" item="job" open="(" separator="," close=")"> #{job} </foreach> </select>
5、UserService.java
//业务层接收表单数据即可 JSONArray getValues(UserForm userForm);
6、UserServiceImpl.java
public JSONArray getValues(UserForm userForm) { UserEntity userEntity = new UserEntity(); List<String> job = userForm.getJob(); List<String> sdkVers = new ArrayList<>(); //这个可以把userForm相同的参数的值传给entity BeanUtils.copyProperties(userForm, userEntity); List<UserEntity> valueList = userMapper.getValues(userEntity,userForm); JSONArray values = new JSONArray(); for(UserEntity userEntity1 : valueList){ values.add(userEntity1); } return values; }
7、controller类就不写了。。。上面的是关键
慢慢学习吧。希望每天都有不一样的收获。