三、数据库操作(转)
来源:互联网 发布:韩国网络电视直播 编辑:程序博客网 时间:2024/06/11 10:16
在转载的第一篇博客中已经写了利用mybatis-generator自动生成映射的包(http://blog.csdn.net/zyf2333/article/details/77623537),这里继续来看看其生成的数据库操作以及mapper.xml。然后我们自己添加一些方法,为下一篇的“注册登录”打下基础
参考并转载的出处:(http://blog.csdn.net/lxfhahaha/article/details/67776108)
首先看看它自动生成的mapper.xml,很容易,都是mybatis的基础内容(不清楚用法的小伙伴可以看看我以前的学习笔记http://blog.csdn.net/zyf2333/article/details/77676090)
那么接下来,就让我们手动添加测试代码(一定要去做,后面的“注册登录”内容会要用到这些自己写的代码)
1)准备工作
首先修改之前创立的user表。设计图如下,设计好了后自己随便添加点内容就好了,用来测试
2)添加三个功能
我们手动添加三个功能, 分别是:查询数据表中的数据总量,准确查询记录,模糊查询记录
修改UserMapper.xml
<!--数量查询--> <select id="getCount" resultType="java.lang.Integer"> select count(*)from user </select> <!--准确查询--> <select id="selectSelective" resultMap="BaseResultMap" parameterType="com.springmvc.entity.User" > select <include refid="Base_Column_List" /> from user <where> <if test="id != null" > id = #{id,jdbcType=INTEGER} </if> <if test="name != null" > AND name = #{name,jdbcType=VARCHAR} </if> <if test="pw != null" > AND pw = #{pw,jdbcType=VARCHAR} </if> <if test="createtime != null" > AND createtime = #{createtime,jdbcType=TIMESTAMP} </if> </where> </select> <!--模糊查询--> <select id="selectLike" resultMap="BaseResultMap" parameterType="com.springmvc.entity.User"> select <include refid="Base_Column_List"/> from user <where> <if test="id != null and id != ''"> AND id LIKE concat('%',#{id},'%') </if> <if test="name != null and name != ''"> AND name LIKE concat('%',#{name},'%') </if> <if test="pw != null and pw != ''"> AND pw LIKE concat('%',#{pw},'%') </if> <if test="createtime != null and createtime != ''"> AND createtime LIKE concat('%',#{createtime},'%') </if> </where> </select>
如图:
修改UserMapper.java
添加对应的三个方法,如图:
修改User.java
由于我们修改了数据表,所以要加上Pw和createtime的setter和getter,并且要重写toString,不然后面测试时候可能没法输出
package com.springmvc.entity;import java.util.Date;public class User { private Integer id; private String name; private String pw; private Date createtime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pw='" + pw + '\'' + ", createtime=" + createtime + '}'; }}
进入UserMapperTest进行测试
代码如下:
//查询数量 @Test public void getCount() throws Exception{ System.out.println(userMapper.getCount()); } //准确查询 @Test public void selectSelective() throws Exception{ User user=new User(); //此处的setPw("565")即查询我数据表中,密码为565的那一列,你可以自己查询id或者名字 user.setPw("565"); ArrayList<User> list=userMapper.selectSelective(user); for (User a:list){ System.out.println(a); } } //模糊查询,查询Name中包含了2的数据 @Test public void selectLike() throws Exception{ User user=new User(); user.setName("2"); ArrayList<User> list=userMapper.selectLike(user); for (User a:list){ System.out.println(a); } }
如图:
最后自己利用JUnit测试即可,让我们看看效果:
首先是查询数量:可见,有十条数据
接下来看看准确查询,我是查询密码为“565”的数据:
最后看看模糊查询
- 三、数据库操作(转)
- 【转】数据库基本知识:(三)数据操作-增
- MongoDB 数据库操作(三)-高级查询
- java操作数据库sqlserver(三)
- 数据库系统操作备忘(三)
- JDBC连接数据库操作(三)
- Django(三) ORM 数据库操作
- SQL 数据库的操作(三)
- mysql数据库基本操作(三)
- SQLite 数据库操作(三)
- mysql(三)操作数据库
- android数据库操作(三)-----用listview显示数据库数据
- Android数据库GreenDAO3.2.2的使用(三,GreenDao操作外部数据库----操作.db数据库)
- (三) MySQL数据库基本操作
- SQL Server数据库操作(三)
- ADO.NET操作数据库(三)
- 后台学习三---数据库操作
- 数据库组件 Hxj.Data (三)(查询操作篇)
- 51nod 1624 取余最长路(二分)
- Linux学习-->如何通过Shell脚本实现发送邮件通知功能?
- Cheese Aizu
- 区块链技术:第五轮颠覆性革命浪潮
- Vim一键编译运行
- 三、数据库操作(转)
- GifCam2.0使用
- CentOS Linux解决Device eth0 does not seem to be present
- wx.config的生成签名补充
- ItelliJ IDEA 打包机 jar,out 文件夹下没有jar包的
- 第九篇: JAVA文件监听
- Python 循环嵌套
- 按值传递与按引用传递,引用能改是什么?
- gradle repostorices