知道前后台如何交互之后,我们来讲讲对数据库的操作,对数据的操作无非增删改查四种,我们用mybatis-generator逆向生成映射及表之后,就可以用生成的相应函数进行操作。
2.数据库接口
2.1 生成的函数讲解
自动生成的映射,包含以下六种数据库接口函数
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
他们对应的sql源码在sqlmap里面,自己可以试着理解一下
2.1.1 int deleteByPrimaryKey(Integer id)
这个函数的意思是通过对象的主键来删除数据,因为主键是唯一不变的,所以我们只要知道了主键,就能使用这个函数来删除数据库里的那条数据,返回值是int,0是失败,1是成功
2.1.2 int insert(User record)
这个函数的意思是向数据库里插入数据,参数就是这个类的对象,要注意的是,想要成功的插入数据,那么在插入的时候,所有数据库设置为非空的属性,我们都要在对象里设置好,比如user有name、create_time两个非空属性,那么insert的时候,我们要先设置好值,才能成功insert,而id一般是int自增的,所以不设置,返回值是int,0是失败,1是成功
2.1.3 int insertSelective(User record)
这个函数跟上面那个差不多,都是插入,有区别的是它插入的时候加上了判断,如果某个属性为NULL,则插入数据库的时候,那个属性不赋值;而上面那个insert()函数却是在插入的时候把所有的属性都赋值给数据库,null也赋值为null,两个实际效果是一样的
2.1.4 User selectByPrimaryKey(Integer id)
这个函数的意思是通过主键获得对应数据,输入对象的id,然后就能获得这个对象的具体信息,查询成功的话返回对象,失败的话返回null
2.1.5 int updateByPrimaryKeySelective(User record)
这个函数的意思是通过主键来更新,且只有record里面不是null的属性,才能对数据库进行更新操作,比如说,我现在数据库里有这么一条数据User(id=1,name=lxf,password=123456),我现在想对他进行更新record(id=1,name=lxf123,password=null),执行函数,那么数据库里的记录将变为User(id=1,name=lxf123,password=123456)
2.1.6 int updateByPrimaryKey(User record)
这个函数的意思是通过主键来更新,且record里面的所有属性都将对数据库进行更新操作,比如说,我现在数据库里有这么一条数据User(id=1,name=lxf,password=123456),我现在想对他进行更新record(id=1,name=lxf123,password=null),执行函数,那么数据库里的记录将变为User(id=1,name=lxf123,password=null)
以上函数的效果大家可以生成junit测试一下,不会测试的可以参考我这篇博客-后台学习一—spring+maven+mybatis+mysql+junit整合 ,里面有讲到的
2.2自定义数据库接口
上面的数据库接口都是mybatis自动生成的基本接口,但有时候我们想实现特殊的操作,那该怎么办呢?这个时候我们就可以根据自己的需求写特定的数据库接口,这里,我讲三个函数。
2.2.1 int getCount()
这个函数用来返回数据库对应表里有多少数据,先在sqlmap文件夹下的映射文件userMapper.xml里面写sql代码,我想知道我的user表里有多少数据,所以代码这么写
<select id="getCount" resultType="java.lang.Integer">
select count(*)from user
</select>
- 1
- 2
- 3
然后到数据库接口文件,dao目录下,userMapper.java里面,将函数加上就可以了
好了,现在测试看看这个函数行不行,junit测试
ok,我user表里面的确有两条数据。
2.2.2 ArrayList < User > selectSelective(User record)
这个函数可以查询符合某一条件的所有记录,比如所我想查询所有name="lxf"
的记录,只要在record里面record.setName("lxf")
,然后用这个函数就能查出并返回相应集合。我们先在sqlmap文件夹下的映射文件userMapper.xml里面写sql代码
<select id="selectSelective" resultMap="BaseResultMap" parameterType="com.springmvc.lxf.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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然后到数据库接口文件,dao目录下,userMapper.java里面,将函数加上就可以了
好了,现在测试看看这个函数的实际效果,junit测试,首先,这是我数据库里的记录
然后写方法测试,输出看看
ok,正确的拿到了数据。
这里要注意的一点是,如果你输出的结果是这样的
出现这样子是因为你没有重载实体类的toString()
方法,你数据正确拿到了,就是输出显示有问题,我们只要去entity包下的User.java
重载toString()
方法就可以了,鼠标右键generate
这样子再运行就能正确输出了
2.2.3 ArrayList < User > selectLike(User record)
这个函数跟上面那个差不多,但是支持模糊查询,比如说我record.setName("lxf")
,然后执行该函数,那么将返回所有name包括lxf
的记录,诸如lxf123、123lxf
之类的,都将被返回。我们先在sqlmap文件夹下的映射文件userMapper.xml里面写sql代码
<select id="selectLike" resultMap="BaseResultMap" parameterType="com.springmvc.lxf.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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然后到数据库接口文件,dao目录下,userMapper.java里面,将函数加上就可以了
好了,现在测试看看这个函数的实际效果,junit测试,首先,这是我数据库里的记录
然后写方法测试,输出看看
ok,正确的拿到了数据。
好了,三个函数讲解完了,你们也可以自己去试着写些有特殊需求的接口函数,这个就要对sql知识有一定的了解了
2.3使用,联系前台
之前都是热身,现在才是重头戏了,后台的操作无非就是数据的增删改查,一切的准备都是为了和前台联系上,那么现在怎么联系前台呢?
现在,我们有了数据库接口,但是实际使用的时候,我们是不会直接使用这个数据库接口的,我们要给他封装一下,加个service层,底层数据库接口是不变的,那么想要满足业务需求,一直在变的就是service层的函数
我们现在在service包里新建一个UserService.java
,跟userMapper.java
里面要加@Repository
来表明他是数据库接口的身份一样,我们在这个文件也要写个@Service
来表明他是服务层的身份,然后自动注入mapper,然后在里面添加我们需要用到的函数
我们可以把mapper文件里面的函数都在这封装一下
package com.springmvc.lxf.service;
import com.springmvc.lxf.dao.UserMapper;
import com.springmvc.lxf.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
/**
* Created by 11655 on 2017/3/29.
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int insert(User user) {
return userMapper.insertSelective(user);
}
int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}
User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}
int updateByPrimaryKey(User record) {
return userMapper.updateByPrimaryKey(record);
}
int getCount() {
return userMapper.getCount();
}
ArrayList<User> selectSelective(User record) {
return userMapper.selectSelective(record);
}
ArrayList<User> selectLike(User record) {
return userMapper.selectLike(record);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
好了,我们现在就可以在controller文件里面使用service里面的函数来操作数据库了,比如说现在我们想实现在userPost页面上输入user的信息,然后form post到后台,接着用数据库接口保存到数据库里
userPost.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<label for="form2">表单传值,user对象传到后台,注意,< input > 的 name 对应后台user的属性</label>
<form id="form2" action="/lxf/test3/postUser" method="post">
Name:<input type="text" name="name"><br>
Password: <input type="text" name="pw"><br>
<input type="submit" value="点击提交">
</form>
<p>反馈信息:${result}</p>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
后台controller页面
我们先@Autowired
自动注入service,然后就能使用service里面的方法
package com.springmvc.lxf.controller;
import com.springmvc.lxf.entity.User;
import com.springmvc.lxf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.Date;
/**
* Created by 11655 on 2017/3/29.
*/
@Controller
public class Test3Controller {
@Autowired
private UserService userService;
//跳转到user提交页面
@RequestMapping(value = "/lxf/test3/postUser", method = RequestMethod.GET)
public String toUserPost() {
return "lxf/userPost";
}
//提交,保存user
@RequestMapping(value = "/lxf/test3/postUser", method = RequestMethod.POST)
public String doUserPost(@ModelAttribute User user, Model model) {
user.setCreatetime(new Date());
String result;
if (userService.insert(user) == 1)
result = "插入成功!";
else
result = "插入失败!";
model.addAttribute("result",result);
return "lxf/userPost";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
好了,看看页面效果
ok,其他方法的使用大同小异,大家可以自己尝试一下
如果我有哪里讲得不够好或者有问题的地方,欢迎大家提出来共同进步~
欢迎加入–qq群–JAVA后台学习交流群:486055993