最近爱上了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应用了,这个我会在将来的博客中继续讲解。也是继续在这个项目的基础上,希望能帮助到大家。
如果哪里有问题,请即时指出,我会立即修改,以免误人子弟了。