首页 > 代码库 > 一个完整的mybatis项目,包含增删改查

一个完整的mybatis项目,包含增删改查

1、导入jar包,导入相关配置文件,均在自己博客园的文件中

编写mybatis.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"./mybatis-3-config.dtd">
<configuration>
<!-- properties配置文件中属性值,在整个配置文件中通过${}进行引用 -->
<properties>
	<property name="driver" value="http://www.mamicode.com/com.mysql.jdbc.Driver" />
</properties>  
  
<!-- 数据源环境信息配置 -->  
<environments default="development">
	<environment id="development">
		<transactionManager type="JDBC" />
		<dataSource type="POOLED">
			<!-- oracle数据源配置 -->
			<!-- <property name="driver" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver" /> -->
			<!-- <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@192.168.1.34:1521:orcl" /> -->
			<!-- <property name="username" value="http://www.mamicode.com/scott" /> -->
			<!-- <property name="password" value="http://www.mamicode.com/tiger" /> -->
			<!-- mysql数据源配置 -->
			<property name="driver" value="http://www.mamicode.com/${driver}" />
			<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost/ys" />
			<property name="username" value="http://www.mamicode.com/root" />
			<property name="password" value="http://www.mamicode.com/admin" />
		</dataSource>
	</environment>
</environments>

<mappers>
	<mapper resource="com/wh/mapper/DeptMapper.xml" />
</mappers>

</configuration>

编写单例模式的mybatis测试类

package com.wh.mapperImpl;
/**
 * 将mybatis中事务管理这一块,用单例模式实现
 */
import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class BaseDao {

	private static SqlSessionFactory ssf;
	public SqlSession ss;

	static {
		String resource = "mybatis.xml";
		try {
			// 读取配置文件
			InputStream in = Resources.getResourceAsStream(resource);
			// 创建连接工厂
			ssf = new SqlSessionFactoryBuilder().build(in);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 获得连接
	public SqlSession openSession() {
		if (ss == null) {
			// 事务自动提交,默认是false不自动提交   true自动提交
			ss = ssf.openSession(true);
		}
		return ss;
	}

	// 提交
	public void commit() {
		if (ss != null) {
			ss.commit();
		}
	}

	// 回滚
	public void rollback() {
		if (ss != null) {
			ss.rollback();
		}
	}

	// 关闭连接
	public void close() {
		if (ss != null) {
			ss.close();
		}
	}
}

2、编写Dept实体类

package com.wh.pojo;

public class Dept {
	private Integer dpt_id;
	private String dpt_name;
	private String dpt_ioc;

	public Dept() {
		// TODO Auto-generated constructor stub
	}

	public Dept(Integer dpt_id, String dpt_name, String dpt_ioc) {
		super();
		this.dpt_id = dpt_id;
		this.dpt_name = dpt_name;
		this.dpt_ioc = dpt_ioc;
	}

	public Integer getDpt_id() {
		return dpt_id;
	}

	public void setDpt_id(Integer dpt_id) {
		this.dpt_id = dpt_id;
	}

	public String getDpt_name() {
		return dpt_name;
	}

	public void setDpt_name(String dpt_name) {
		this.dpt_name = dpt_name;
	}

	public String getDpt_ioc() {
		return dpt_ioc;
	}

	public void setDpt_ioc(String dpt_ioc) {
		this.dpt_ioc = dpt_ioc;
	}

	@Override
	public String toString() {
		return "Dept [dpt_id=" + dpt_id + ", dpt_name=" + dpt_name + ", dpt_ioc=" + dpt_ioc + "]";
	}
}

3、编写DeptMapper接口

package com.wh.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.wh.pojo.Dept;

public interface DeptMapper {

	public void insertDept(Dept dept);

	public List<Dept> selectAll();

	public Dept selectById(Integer id);

	public void updateDept(Dept dept);

	public void deleteDept(Integer id);
	
	public List<Dept> selectByName(String name);
	
	public List<Dept> selectByMore(@Param("dpt_name")String dpt_name,@Param("dpt_ioc")String dpt_ioc);

	public List<Dept> selectByList(@Param("ids") List<Integer> ids);
}

4、导入mybatis-3-mapper.dtd文件

5、编写DeptMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"./mybatis-3-mapper.dtd">
<!--namespace 绑定接口   -->	
<mapper namespace="com.wh.mapper.DeptMapper"> 

	<!-- 插入  parameterType 参数类型-->
	<insert id="insertDept" parameterType="com.wh.pojo.Dept"> 
		insert into dept values (#{dpt_id},#{dpt_name},#{dpt_ioc});
	</insert>

    <!--查询所有        id 接口中的方法名          resultType 返回结果类型-->
	<select id="selectAll" resultType="com.wh.pojo.Dept"> 
		select * from dept
	</select>
	
	<!-- 单个查询 -->
	<select id="selectById" parameterType="java.lang.Integer"  resultType="com.wh.pojo.Dept"> 
		select * from dept where dpt_id = #{id}
	</select>
	
	<!-- 修改 -->
	<update id="updateDept" parameterType="com.wh.pojo.Dept"> 
		update dept set dpt_name=#{dpt_name},dpt_ioc=#{dpt_ioc} where dpt_id = #{dpt_id}
	</update>
	
	<!-- 删除 -->
	<delete id="deleteDept" parameterType="java.lang.Integer"> 
		delete from dept where dpt_id=#{dpt_id}
	</delete>

	<!-- 模糊查询  -->
	<select id="selectByName" parameterType="java.lang.String" resultType="com.wh.pojo.Dept"> 
		select * from dept where dpt_name like concat(‘%‘,#{dpt_name},‘%‘)
	</select>
	
	<!-- 多重条件查询   接口要对形参注解          concat(‘%‘,‘销售‘,‘%‘)   -->
	<select id="selectByMore" resultType="com.wh.pojo.Dept"> 
		select * from dept where 1=1 
		<if test="dpt_name!=null and dpt_name!=‘‘ ">
			and dpt_name like concat(‘%‘,#{dpt_name,jdbcType=VARCHAR},‘%‘)
		</if>
		<if test="dpt_ioc!=null and dpt_ioc!=‘‘ ">
			and dpt_ioc like concat(concat(‘%‘,#{dpt_ioc,jdbcType=VARCHAR}),‘%‘)
		</if>
	</select>
	
	<!-- 集合查询   in -->
	<select id="selectByList" resultType="com.wh.pojo.Dept"> 
		select * from dept where dpt_id in
		<foreach collection="ids" index="index" open="(" separator="," close=")" item="id">
			#{id}
		</foreach>
	</select>
	
	<!-- 分页查询  -->
</mapper>

6、编写DeptMapperImpl实现类

package com.wh.mapperImpl;

import java.util.List;

import com.wh.mapper.DeptMapper;
import com.wh.pojo.Dept;


public class DeptDaoImpl extends BaseDao implements DeptMapper {

	@Override
	public void insertDept(Dept dept) {
		//获得连接
		this.openSession();
		//找到接口      获得bean 映射关系   绑定实体类与表列名
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		mapper.insertDept(dept);
	}

	@Override
	public List<Dept> selectAll() {
		//获得连接
		this.openSession();
		//找到接口
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		return mapper.selectAll();
	}

	@Override
	public Dept selectById(Integer id) { 
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		return mapper.selectById(id);
	}

	@Override
	public void updateDept(Dept dept) {
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		mapper.updateDept(dept);
	}

	@Override
	public void deleteDept(Integer id) {
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		mapper.deleteDept(id);
	}

	@Override
	public List<Dept> selectByName(String name) {
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		return mapper.selectByName(name);
	}

	@Override
	public List<Dept> selectByMore(String dpt_name, String dpt_ioc) {
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		return mapper.selectByMore(dpt_name,dpt_ioc);
	}

	@Override
	public List<Dept> selectByList(List<Integer> ids) {
		this.openSession();
		DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
		return mapper.selectByList(ids);
	}

}

7、编写测试类

package com.wh.junit;
/**
 * mybatis编写顺序
 * DeptMapper.java、DeptMapper.xml、DeptDaoImpl.java、TestMyBatis.java
 */
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.wh.mapperImpl.DeptDaoImpl;
import com.wh.pojo.Dept;

public class TestMyBatis {

	//mybatis快速入门
	@Test
	public void test00() throws IOException{
		InputStream in = Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(in);
		SqlSession ss=ssf.openSession();
		String string = ss.toString();
		System.out.println(string); 
	}
	
	//插入
	@Test
	public void testInsertDept() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		dao.insertDept(new Dept(4,"技术","4楼"));
	}
	
	//查询所有
	@Test
	public void testSelectAll() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		List<Dept> list = dao.selectAll();
		System.out.println(list.size()); 
		System.out.println(list); 
	}
	
	//查询单个
	@Test
	public void testSelectById() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		Dept d = dao.selectById(3);
		System.out.println(d); 
	}
	
	//修改
	@Test
	public void testUpdateDept() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		dao.updateDept(new Dept(3,"情报部","xxx"));
	}
	
	//删除
	@Test
	public void testdeleteDept() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		dao.deleteDept(3);
	}
	
	//模糊查询
	@Test
	public void testselectByName() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		List<Dept> list = dao.selectByName("销");
		System.out.println(list);
 	}
	
	//多重条件查询
	@Test
	public void testSelectByMore() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		List<Dept> list = dao.selectByMore("销","2");
		System.out.println(list);
	}
	
	//集合查询   in
	@Test
	public void testSelectByList() throws IOException{
		DeptDaoImpl dao=new DeptDaoImpl(); 
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(1);
		ids.add(3);
		List<Dept> list = dao.selectByList(ids);
		System.out.println(list);
	}
}

  

 

一个完整的mybatis项目,包含增删改查