知道前背景如何互動之後,我們來講講對資料庫的操作,對資料的操作無非增删改查四種,我們用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) == )
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