最近愛上了Mybatis,因爲他的動態sql,因爲他的輕量,因爲開發很敏捷。依賴Mybaits,你可以少寫很多Service接口。今天,就來給大家分享一下。
項目使用Maven構建。主要是因爲github不推薦傳jar包,而且非maven項目會因爲IDE之間的差異而導致打不開別人的項目,比如我IntelliJ創建的項目,你Eclipse無法導入。如果實在對maven不瞭解也不想了解的騷年,看一看這裏的代碼,也應該能明白的。
項目已經創建好了,創建方法可以見我的這篇博客:http://blog.csdn.net/qj30212/article/details/52420363
源碼已經同步在github上:https://github.com/qjkobe/Mybatis
首先創建一個數據庫Mybatis並創建數據表test:
創建如下四個目錄(SpringMVC配置可以參考別的教程,也可以參考我的源碼,此處不再贅述)
然後就是使用Mybatis-generator生成基本的CRUD操作。生成方法可以Maven+Idea,此處限於篇幅不贅述(以後可能會單獨寫博)。也可以使用eclipse生成以後,copy過來。
生成完默認的以後,我們需要做一些操作,首先,創建一個Pojo,裏面什麼都不用寫(主要是爲了後面的BaseMapper)
然後讓Test繼承這個Pojo
然後創建BaseMapper並讓TestMapper繼承它
然後,在TestMapper.xml的最下方,實現BaseMapper裏的方法
<select id="selectListByParam" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from test
where 1 = 1
<if test="pojo != null and pojo.name != null and pojo.name != ''">
and name = #{pojo.name,jdbcType=VARCHAR}
</if>
<if test="pojo != null and pojo.age != null">
and age = #{pojo.age,jdbcType=INTEGER}
</if>
<if test="pojo != null and pojo.createtime != null">
and createTime = #{pojo.createtime,jdbcType=TIMESTAMP}
</if>
</select>
<select id="selectCountByParam" resultType="int">
select count(1)
from test
where 1 = 1
<if test="pojo != null and pojo.name != null and pojo.name != ''">
and name = #{pojo.name,jdbcType=VARCHAR}
</if>
<if test="pojo != null and pojo.age != null">
and age = #{pojo.age,jdbcType=INTEGER}
</if>
<if test="pojo != null and pojo.createtime != null">
and createTime = #{pojo.createtime,jdbcType=TIMESTAMP}
</if>
</select>
注意大小寫。java實體都是小寫的,而數據庫的字段可能是大寫的
然後就可以寫Service層了,創建interface:TestService,代碼如下:
package services;
import db.model.Test;
import java.util.List;
/**
* Created by Administrator on 2016/9/3.
*/
public interface TestService {
public List<Test> getTestListByParam(Test test);
public void addTest(Test test);
public void modifyTest(Test test);
public Test getTestById(String id);
}
然後是TestService的實現類:TestServiceImpl,實現的代碼非常簡單:
package services.impl;
import db.dao.TestMapper;
import db.model.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.TestService;
import java.util.List;
/**
* Created by Administrator on 2016/9/3.
*/
@Service("testService")
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
TestMapper testMapper;
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public List<Test> getTestListByParam(Test test) {
return testMapper.selectListByParam(test);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void addTest(Test test) {
testMapper.insertSelective(test);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void modifyTest(Test test) {
testMapper.updateByPrimaryKeySelective(test);
}
@Override
@Transactional(readOnly = true)
public Test getTestById(String id) {
return testMapper.selectByPrimaryKey(id);
}
}
至此,我們就算寫完了基礎的操作了,可以來實驗一下:
新建Junit測試類。因爲沒有啓動Tomcat和spring容器,只是單元測試,所以沒有自動注入,我們就手動注入一下bean,代碼如下:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import services.TestService;
import java.util.Date;
import java.util.List;
/**
* Created by Administrator on 2016/9/3.
*/
public class Junit {
@Test
public void TestAdd(){
String[] paths = new String[] { "spring/applicationContext-bo.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
TestService testService = (TestService) ctx.getBean("testService");
db.model.Test test = new db.model.Test();
test.setId("AB123");
test.setName("qjkobe");
test.setAge(20);
test.setCreatetime(new Date());
testService.addTest(test);
test.setId("CD456");
test.setName("qjkobe");
test.setAge(21);
test.setCreatetime(new Date());
testService.addTest(test);
}
@Test
public void TestUpdate(){
String[] paths = new String[] { "spring/applicationContext-bo.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
TestService testService = (TestService) ctx.getBean("testService");
db.model.Test test = new db.model.Test();
test.setId("AB123");
test.setAge(22);
testService.modifyTest(test);
}
@Test
public void TestSelect(){
String[] paths = new String[] { "spring/applicationContext-bo.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
TestService testService = (TestService) ctx.getBean("testService");
db.model.Test test = new db.model.Test();
test.setName("qjkobe");
List<db.model.Test> list1 = testService.getTestListByParam(test);
for(db.model.Test x : list1){
System.out.println(x.getId() + ":" + x.getName() + ":" +x.getAge());
}
System.out.println("---------------");
db.model.Test test2 = new db.model.Test();
test2.setAge(21);
List<db.model.Test> list2 = testService.getTestListByParam(test2);
for(db.model.Test x : list2){
System.out.println(x.getId() + ":" + x.getName() + ":" +x.getAge());
}
}
}
先測試一下添加功能
測試成功,看看數據庫:
數據成功被添加。再試試更新功能
年齡成功變成了22.
最後來試試select功能:
返回了正確的結果。
是不是很方便呢。只要傳入實體類就可以增加查詢修改,而且也不需要自己去寫複雜的sql語句呢。
接下來就是Controller層和web應用了,這個我會在將來的博客中繼續講解。也是繼續在這個項目的基礎上,希望能幫助到大家。
如果哪裏有問題,請即時指出,我會立即修改,以免誤人子弟了。