# Mybatis使用之簡單的增刪改查
### 一:簡介
????
?????? 主要記錄最簡單的數據的增刪改查、下一章會有各個操作詳細一點的配置說明。以Author表為例(見上一博客)、Author表沒有關聯任何其他表、也沒有特殊字段。
### 二:映射規則
### ???????2.1、映射文件中的sql方法與對應的XxxMapper接口中的方法映射規則:
?????????????a)映射文件的namespace的值是XxxMapper接口的全限定名、即包名+接口名稱
?????????????b)映射文件中表示增刪改查的標簽(select、insert、delete、update)的id的值是接口中方法名(id具有唯一性)
?????????????c)映射文件中增刪改查標簽的表示參數的屬性指定的值對應接口中方法的參數類型
?????????????d)映射文件中增刪改查標簽的表示返回值的屬性指定的值對應接口中方法的參數類型
### ???????2.2、舉例說明:
?????????????現在需要根據一個int型的id來查詢一個作者的具體信息。
?????????????a)AuthorMapper中定義此方法
~~~
<span style="color:#FF0000;">package org.alien.mybatis.samples.mapper;</span>
public interface AuthorMapper {
Author <span style="color:#FF0000;">getAuthorById</span>(int id);
}
~~~
?????????????b)AuthorMapper.xml中關聯此方法
~~~
<pre name="code" class="html"><span style="color:#FF0000;"><mapper namespace="org.alien.mybatis.samples.mapper.AuthorMapper"></span>
<select id="<span style="color:#FF0000;">getAuthorById</span>" parameterMap="int" resultType="author">
SELECT
t.id,
t.username,
t.password,
t.email,
t.bio,
t.favourite_section favouriteSection
FROM author t
WHERE t.id=#{id}
</select>
~~~
~~~
~~~
?????????????c)執行方法(見下文)
### 三:增刪改查實現
????
### ???????3.1、流程圖:
????
?????????????3.1.1 開發流程圖(順序可不同)

?????????????3.1.2 執行UML序列圖

### ???????3.2、具體實現過程:
????
?????????????3.2.1、mybatis配置文件配置
?????????????詳細信息見上一篇、這里重點對一下兩點進行說明
?????????????a)自動掃描指定包下面所有JavaBean、并將其類名首字母小寫作為key來代表當前JavaBean、用于映射文件配置增刪改查標簽時入參或者返回值類型是此JavaBean類型的結果集的時候。比如Author。當根據id查詢某一個具體的Author的信息的時候、select標簽的resultType的值就可以直接使用author來代替Author全路徑名。入參同樣。具體配置片段:
~~~
<typeAliases>
<!--That is domain.blog.Author will be registered as author. If the @Alias annotation is found its value will be
used as an alias.@Alias("author")-->
<package name="org.alien.mybatis.samples.model"/>
</typeAliases>
~~~
?????????????b)自動 掃描指定包下面所有的映射文件、并加載到內存中。這樣配置的好處就是不用為每一個映射文件都要配置一次。具體配置片段:
~~~
<mappers>
<package name="org.alien.mybatis.samples.mapper"/>
</mappers>
~~~
?????????????3.2.2、AuthorMapper.xml映射文件配置
?????????????a)添加
?????????????通過insert標簽定義
?????????????b)查詢
?????????????通過select標簽定義
?????????????c)修改
?????????????通過update標簽定義
?????????????d)刪除
?????????????通過delete標簽定義
~~~
<mapper namespace="org.alien.mybatis.samples.mapper.AuthorMapper">
<select id="getAuthorById" parameterMap="int" resultType="author">
SELECT
t.id,
t.username,
t.password,
t.email,
t.bio,
t.favourite_section favouriteSection
FROM author t
WHERE t.id=#{id}
</select>
<insert id="addAuthor" parameterType="author">
INSERT INTO author(id, username) VALUES (#{id}, #{username})
<selectKey keyProperty="id" resultType="int">
SELECT max(id) FROM author
</selectKey>
</insert>
<delete id="deleteAuthor" parameterType="int">
DELETE FROM author
WHERE id = #{id}
</delete>
<update id="updateAuthor" parameterType="author">
UPDATE author
SET username = #{username}
</update>
<select id="getAllAuthors" resultType="author">
SELECT
t.id,
t.username,
t.password,
t.email,
t.bio,
t.favourite_section favouriteSection
FROM author t
</select>
<select id="getAllAuthorsCount" resultType="int">
SELECT count(1)
FROM author
</select>
</mapper>
~~~
?????????????3.2.3、編寫AuthorMapper接口以及方法
~~~
package org.alien.mybatis.samples.mapper;
import org.alien.mybatis.samples.model.Author;
import java.util.List;
public interface AuthorMapper {
int addAuthor(Author author);
int deleteAuthor(int id);
int updateAuthor(Author author);
List<Author> getAllAuthors();
int getAllAuthorsCount();
Author getAuthorById(int id);
}
~~~
?????????????3.2.4 加載配置文件、調用AuthorMapper方法
~~~
package org.alien.mybatis.samples.service.impl;
import org.alien.mybatis.samples.mapper.AuthorMapper;
import org.alien.mybatis.samples.model.Author;
import org.alien.mybatis.samples.service.AuthorService;
import org.alien.mybatis.samples.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
/**
* Created by andychen on 2015/5/18.<br>
* Version 1.0-SNAPSHOT<br>
*/
public class AuthorServiceImpl implements AuthorService {
/**
* Add author info.
*
* @param author author instance
* @return The key of current record in database.
*/
@Override
public int addAuthor(Author author) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
int authorId = authorMapper.addAuthor(author);
sqlSession.commit();
return authorId;
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
/**
* Delete author info by author's id.
*
* @param authorId author id
* @return int The number of rows affected by the delete.
*/
@Override
public int deleteAuthor(int authorId) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
int result = authorMapper.deleteAuthor(authorId);
sqlSession.commit();
return result;
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
/**
* update author info
*
* @param author Author instance
* @return int The number of rows affected by the update.
*/
@Override
public int updateAuthor(Author author) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
int result = authorMapper.updateAuthor(author);
sqlSession.commit();
return result;
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
/**
* Query all authors
*
* @return all authors
*/
@Override
public List<Author> getAllAuthors() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
return authorMapper.getAllAuthors();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
/**
* Query all authors count
*
* @return all authors count
*/
@Override
public int getAllAuthorsCount() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
return authorMapper.getAllAuthorsCount();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
@Override
public Author getAuthorById(int id) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
AuthorMapper authorMapper = sqlSession.getMapper(AuthorMapper.class);
return authorMapper.getAuthorById(id);
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
~~~
### 四:補充
?????? 更多內容:[Mybatis 目錄](http://blog.csdn.net/crave_shy/article/details/45825599)
?????? github地址:https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis
?????? 源碼下載地址:http://download.csdn.net/detail/chenghuaying/8713311
?????? MybatisUtil:見上一篇博客