发表于: 2018-05-20 22:22:49
1 1367
今天完成的事情:
新建一张表,更改了昨天的程序
更改的UserMapper.xml
<!--prefix:在trim标签内sql语句加上前缀。-->
<!--suffix:在trim标签内sql语句加上后缀。-->
<!--suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",
去除trim标签内sql语句多余的后缀","。-->
<!--prefixOverrides:指定去除多余的前缀内容-->
<mapper namespace="com.fgh.Mybatis.dao.UserMapper" >
<sql id="findBy">
<trim suffixOverrides=",">
<if test="username!=null and username!=''"> username=#{username},</if>
<if test="id!=null and id!=''"> id=#{id},</if>
<if test="QQ!=null and QQ!=''"> QQ=#{QQ},</if>
<if test="type!=null and type!=''"> type=#{type},</if>
<if test="joinTime!=null and joinTime!=''"> joinTime=#{joinTime},</if>
<if test="school!=null and school!=''"> school=#{school},</if>
<if test="onlineId!=null and onlineId!=''"> onlineId=#{onlineId},</if>
<if test="daily!=null and daily!=''"> daily=#{daily},</if>
<if test="description!=null and description!=''"> description=#{description},</if>
<if test="counsellor!=null and counsellor!=''"> counsellor=#{counsellor},</if>
<if test="way!=null and way!=''"> way=#{way},</if>
<if test="create_at!=null and create_at!=''"> create_at=#{create_at},</if>
<if test="update_at!=null and update_at!=''"> update_at=#{update_at}</if>
</trim>
</sql>
<!--自定义查询-->
<select id="findUserBy" parameterType="user" resultType="user">
SELECT * FROM taskone
<where>
<include refid="findBy"/>
</where>
</select>
判断对用字段数据是否为空或者空格,条件满足,执行语句,否则跳过;继续判断下个字段
输入记录相应的数据类型都能检测到,不用多次写Byname/ById,
重写User类
public class User {
private int id;
private String username;
private String QQ;
private String type;
private long joinTime;
private String school;
private String onlineId;
private String daily;
private String description;
private String counsellor;
private String way;
private long create_at;
private long update_at;
private TimeReversal TimeReveral;
get/set省略
更改查询语句id,dao层接口和services实现类和接口也相应更改
public interface UserMapper {
public List<User> findUserBy(User user)throws Exception;
public interface UserService {
public List<User> findUserBy(User user)throws Exception;
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> findUserBy(User user)throws Exception{
return userMapper.findUserBy(user);
}
测试类
@Test
public void testFinderUserBy()throws Exception{
User user = new User();
user.setType("pm");
List<User> users = userService.findUserBy(user);
System.out.println(users);
}
@Test
public void testInsertUser()throws Exception {
User user = new User();
user.setUsername("安师傅2");
user.setQQ("201805121234");
user.setType("QA");
user.setDescription("插入数据2");
user.setCreate_at(TimeReversal.Datetolong(new Date()));
Boolean InsertBool=userService.insertUser(user);
System.out.println(InsertBool);
}
@Test
public void testDelUser()throws Exception {
Boolean DelBool=userService.delUser(4);
System.out.println(DelBool);
}
//
@Test
public void testUpdateUser()throws Exception {
User user = new User();
user.setId(2);
user.setUsername("卡卡");
user.setQQ("201805201126");
user.setCounsellor("东方国际");
user.setDescription("电饭锅");
user.setUpdate_at(TimeReversal.Datetolong(new Date()));
Boolean UpdateBool=userService.updateUser(user);
System.out.println(UpdateBool);
}
@Test
public void tesFindAll()throws Exception {
List<User> users=userService.findAll();
System.out.println(users);
}
执行命令后数据库
明天计划的事情:
学习jar包打包
遇到的问题:
使用findBy Sql片段
更新数据错误
update taskone SET username=?
-- username=?,QQ=?,type=?,joinTime=?,school=?,
-- onlineId=?,daily=?,description=?,counsellor=?,
-- way=?,update_at=? where id=?
Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession@57d7f8ca]
Parameter index out of range (
7 > number of parameters, which is 6)
原因 原Sqle没有注释掉 (通过报错的日志和查找功能,知道执行的那句语句,寻找错误位置)
-- username=#{username},QQ=#{QQ},type=#{type},joinTime=#{joinTime},school=#{school},
-- onlineId=#{onlineId},daily=#{daily},description=#{description},counsellor=#{counsellor},
-- way=#{way},update_at=#{update_at}
正确注释:
<!--
username=#{username},QQ=#{QQ},type=#{type},joinTime=#{joinTime},school=#{school},
onlineId=#{onlineId},daily=#{daily},description=#{description},counsellor=#{counsellor},
way=#{way},update_at=#{update_at}
-->
<!--更新数据-->
<update id="updateUser" parameterType="user" >
update taskone
<set>
<include refid="findBy"/>
</set>
<!--
username=#{username},QQ=#{QQ},type=#{type},joinTime=#{joinTime},school=#{school},
onlineId=#{onlineId},daily=#{daily},description=#{description},counsellor=#{counsellor},
way=#{way},update_at=#{update_at}
-->
where id=#{id}
</update>
打包显示测试类有错误
收获:
Mybatis trim标签用法
使用trim标签在Sql片段中
评论