该文章通过使用mybatis,spring以及mybatis整合spring的简单技术实现一个对数据库进行增删改查的简单操作,
项目结构如下所示:
创建表的sql脚本如下:
CREATE TABLE `shoes` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`price` decimal(10,2) DEFAULT NULL COMMENT '价格',
`name` varchar(100) NOT NULL COMMENT '鞋名',
`color` varchar(100) NOT NULL COMMENT '配色',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
pom.xml文件如下:
4.0.0
com.zhangguo
BookStore
0.0.1
jar
UTF-8
4.3.0.RELEASE
org.springframework
spring-context
${spring.version}
org.aspectj
aspectjweaver
1.8.9
org.springframework
spring-web
${spring.version}
org.mybatis
mybatis-spring
1.3.0
org.springframework
spring-jdbc
${spring.version}
mysql
mysql-connector-java
5.1.38
org.apache.logging.log4j
log4j-core
2.6.1
org.mybatis
mybatis
3.4.1
junit
junit
4.10
c3p0
c3p0
0.9.1.2
javax.servlet
jstl
1.2
如果是第一次依赖相关的包,则需要下载时间,请耐心等待
package com.christophe.shoesstore.pojo;
/***
*
* @author:Christophe0599
*
*/
public class Shoes {
private int id;
private int price;
private String name;
private String color;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Shoes(int id,int price,String name,String color){
this.id=id;
this.price=price;
this.name=name;
this.color=color;
}
public Shoes(){
}
public String toString(){
return "编号 :"+id+"价格 :"+price+"鞋名 :"+name+"配色 :"+color;
}
}
package com.christophe.shoesstore.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.christophe.shoesstore.pojo.Shoes;
/***
*
* @author:Christophe
*
*/
public interface ShoesDAO {
//获取所有鞋子
public List getAllShoes();
//根据id获取鞋子信息
public Shoes getShoesById(@Param("id") int id);
//根据id删除鞋子
public int deleteShoes(int id);
//添加鞋子
public int addShoes(Shoes newshoes);
}
为MyBatis ORM创建的映射文件ShoesMapper.xml
insert into shoes(id,price,name,color)
values(#{id},#{price},#{name},#{color})
delete from books where id=#{id}
在源代码的根目录下新建 db.properties文件,用于存放数据库连接信息,文件内容如下:
#mysql jdbc
jdbc.driver=com.mysql.jdbc.Driver
//自己数据库的连接,建议使用navicat
jdbc.url=jdbc:mysql://
//数据库的用户名和密码
jdbc.uid=
jdbc.pwd=
根目录下新建 applicationContext.xml文件,用于整合MyBatis与Spring,该文件是整个项目的控制中心,非常关键,具体的内容如下:
创建ShoesService服务类,完成鞋子管理业务,有些项目中也叫业务层
package com.christophe.shoesstore.service;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import com.christophe.shoesstore.mapper.ShoesDAO;
import com.christophe.shoesstore.pojo.Shoes;
/***
*
* @author:Christophe0599
*
*/
@Service
public class ShoesService {
@Resource
ShoesDAO shoesdao;
public List getAllShoes(){
return shoesdao.getAllShoes();
}
public Shoes getShoesById( int id){
return shoesdao.getShoesById(id);
}
public int deleteShoes(int id){
return shoesdao.deleteShoes(id);
}
public int addShoes(Shoes newshoes) throws Exception{
if(newshoes.getName()==null||newshoes.getName().equals("")){
throw new Exception("鞋名必须不为空");
}
return shoesdao.addShoes(newshoes);
}
}
package com.christophe.shoesstore.test;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.christophe.shoesstore.pojo.Shoes;
import com.christophe.shoesstore.service.ShoesService;
/***
*
* @author:Christophe0599
*
*/
public class TestsShoes {
static ShoesService shoesservice;
@BeforeClass
public static void before(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
shoesservice = ctx.getBean(ShoesService.class);
}
@Test
public void testGetAllBooks() {
List shoes=shoesservice.getAllShoes();
assertNotNull(shoes);
for (Shoes tmp:shoes) {
System.out.println(tmp.toString());
}
}
到此为止就是仅在后端的一个mybatis和spring整合的简单实例,最终输出结果如下:
ps.此实例仅在后端实现,下一次将在web环境下搭载整合实例