发表于: 2018-04-07 20:51:31
1 666
今天完成的事情:
1.学习使用mybatis动态语句查询
2.添加根据学员学号和名字查找数据库
3.获取插入数据后返回id值,删除/修改后返回boolean
1.mybatis动态语句
单个参数传值:
<select id="selectById" parameterType="int" resultMap="personMap" >
SELECT * FROM person WHERE id = #{id}
</select>
使用动态语句:
<select id="selectById" parameterType="int" resultMap="personMap" >
SELECT * FROM person
<where>
<if test="id !=null">
id = #{id}
</if>
</where>
</select>
运行报错:
There is no getter for property named 'id' in 'class java.lang.Integer'
id修改为_parameter:
<select id="selectById" parameterType="int" resultMap="personMap" >
SELECT * FROM person
<where>
<if test="_parameter!=null">
id = #{id}
</if>
</where>
</select>
或者在mapper接口方法中添加@Param("id")
Person selectById(@Param("id") int id);
使用多个参数传值:
Mapper:
public List<Person> selectByColumn(@Param("name") String name,@Param("stu_num") int stu_num);
service:
public List<Person> selectByColumn(String name,int stu_num) {
return this.getSqlSession().selectList("mybatis.mapper.PersonMapper.selectByColumn");
}
xml:
<select id="selectByColumn" resultMap="personMap">
SELECT * FROM person
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="name!=null">
name = #{name}
</if>
<if test="stu_num!=null">
AND stu_num = #{stu_num}
</if>
</trim>
</select>
测试:
logger.debug("\n" + personMapper.selectByColumn("三师兄",2456).toString() + "\n" );
2.添加根据学员学号和名字查找数据库
跟之前一样,这个是使用实体类传值:
Mapper:
public List<Person> selectByColumn(Person person);
Service:
public List<Person> selectByColumn(Person person) {
return this.getSqlSession().selectList("mybatis.mapper.PersonMapper.selectByColumn");
}
xml:
<select id="selectByPerson" parameterType="mybatis.model.Person" resultMap="personMap">
SELECT * FROM person
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="id!=null and id!=0">
AND id = #{id}
</if>
<if test="name!=null">
AND name = #{name}
</if>
<if test="qq!=null and qq!=0">
AND qq = #{qq}
</if>
<if test="tell!=null and tell!=0">
AND tell = #{tell}
</if>
<if test="pro!=null">
AND pro = #{pro}
</if>
<if test="age!=null and age!=0">
AND age = #{age}
</if>
<if test="create_at!=null and create_at!=0">
AND create_at = #{create_at}
</if>
<if test="update_at!=null and update_at!=0">
AND update_at = #{update_at}
</if>
<if test="email!=null">
AND email = #{email}
</if>
<if test="waikey!=null">
AND waikey = #{waikey}
</if>
</trim>
</select>
测试:
Person person = new Person();
person.setName("大师兄");
logger.debug("\n 123 === " + personMapper.selectByPerson(person).toString() + "\n" );
结果:
遇到的报错类型有:
1.Parameter 'name' not found. Available parameters are [1, 0, param1, param2]
解决:Mapper接口参数里添加@Param
2.Parameter index out of range (3 > number of parameters, which is 2).
解决:参数传值错误,或者换种方法/直接传参或者传对象
3.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE name = '三师兄'
AND stu_num = 2456' at line 2
解决:开始写的时候把<where>标签写在了<trim>里面,这两个标签是同级的,有trim就不需要where标签了
3.获取插入数据后返回id值,删除/修改后返回boolean
xml的插入语句,添加slectkey标签
<insert id="insertPerson" parameterType="mybatis.model.Person" useGeneratedKeys="true" >
INSERT INTO person
(id,create_at,update_at,name,age,sex,pro,tell,qq,email,stu_num,waikey)
values
(#{id},#{create_at},#{update_at},#{name},#{age},#{sex},#{pro},#{tell},#{qq},#{email},#{stu_num},#{waikey})
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
<-- select LAST_INSERT_ID()-->
select @@IDENTITY AS ID
</selectKey>
</insert>
Mapper:
// @Insert("INSERT INTO person (id,create_at,update_at,name,age,sex,pro,tell,qq,email,stu_num,waikey) values (#{id},#{create_at},#{update_at},#{name},#{age},#{sex},#{pro},#{tell},#{qq},#{email},#{stu_num},#{waikey})")
int insertPerson(Person person);
测试:(返回值直接原对象属性中)
@Test
public void insertOne(){
int ins = 0;
ApplicationContext context = new ClassPathXmlApplicationContext(contextxml);
MybatisAction mybatisAction = (MybatisAction) context.getBean("mybatisAction");
Person p = new Person();
// p.setId(40);
p.setName("ooo");
p.setQq(85532);
p.setAge(22);
p.setEmail("7777@12.com");
p.setPro("test就是力量");
p.setSex(1);
p.setTell(138000);
try {
ins = mybatisAction.personMapper.insertPerson(p);
// logger.info( "\n" + mybatisAction.personMapper.insertPerson(p) + "\n");
// Assert.assertEquals(p.getId(),41);
System.out.print("新增数据id:" + p.getId() + "\n");
}catch (Exception e){
e.fillInStackTrace();
System.err.print("新增数据未成功!");
}
}
修改和删除:
@Test
public void updateP(){
boolean fblog = false;
try {
ApplicationContext context = new ClassPathXmlApplicationContext(contextxml);
MybatisAction mybatisAction = (MybatisAction) context.getBean("mybatisAction");
Person p = new Person();
p.setId(40);
p.setName("ooo");
p.setQq(85532);
p.setAge(22);
p.setEmail("7777@12.com");
p.setPro("test就是力量");
p.setSex(1);
p.setTell(138000);
int upd = mybatisAction.personMapper.updatePerson(p);
if(upd > 0){
fblog = true;
System.out.print("\n===更新成功===\n" + fblog);
}
int upd = mybatisAction.personMapper.deletePerson(40);
System.err.print(upd);
if(upd > 0){
fblog = true;
System.out.print("\n===删除成功===\n" + fblog);
}
明天计划的事情:
深度思考
问题和收获:
1、学会使用Mybatis的动态语句,需要仔细些,开始只要添加if标签就报错。看官方文档也没找到哪里错的。
2、Mybatis操作数据库的过程和返回值。删除修改的时候是直接返回的行数,是提交了以后的,在数据库可以查询成功。
3、finaly关键字。
4、一直使用springMybatis,封装好了方法,直接调用类就能操作数据,再加上这几天操作数据库,竟然忘了中间的Sqlsession怎么用的了。。
5、之前的表数据太多了,操作真的是很慢。没有加索引的情况下查询明显感觉到慢多了。
评论