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="com.mysql.jdbc.Driver" />
</properties> <!-- 数据源环境信息配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- oracle数据源配置 -->
<!-- <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> -->
<!-- <property name="url" value="jdbc:oracle:thin:@192.168.1.34:1521:orcl" /> -->
<!-- <property name="username" value="scott" /> -->
<!-- <property name="password" value="tiger" /> -->
<!-- mysql数据源配置 -->
<property name="driver" value="${driver}" />
<property name="url" value="jdbc:mysql://localhost/ys" />
<property name="username" value="root" />
<property name="password" value="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项目,包含增删改查的更多相关文章

  1. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  2. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  3. 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序

    分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图.      1.首先建立一个数 ...

  4. Mybatis入门之增删改查

    Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...

  5. MyBatis -- 对表进行增删改查(基于注解的实现)

    1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1  首先须要定义映射sql的 ...

  6. Spring Boot 使用Mybatis注解开发增删改查

    使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...

  7. 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)

    最近看老罗的视频,跟着完成了利用Java操作MySql数据库的一个框架类JdbcUtils.java,完成对数据库的增删改查.其中查询这块,包括普通的查询和利用反射完成的查询,主要包括以下几个函数接口 ...

  8. [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo

    做一个tableView,包含增删改移动功能,并且修改值的时候,在按home键的时候会自动保存.如果可以的话使者保存自定义的类数组保存到plist中. 实现步骤: 1.创建一个SingleViewAp ...

  9. Mybatis的简单增删改查

    刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...

  10. SpringMVC,MyBatis商品的增删改查

    一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...

随机推荐

  1. 【51NOD1806】wangyurzee的树(Prufer编码,容斥原理,组合计数)

    题意:有n个点和m条限制,每条限制限制了一个点的度数不能为某个数. 求合法的树的个数模10^9+7 n<=10^6 m<=17 思路:WYZ作业 首先m<=17显然是2^m容斥 枚举 ...

  2. Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. hdfs是什么?

    参考:https://www.cnblogs.com/shijiaoyun/p/5778025.html hadoop分布式文件系统 1.hdfs是一个分布式文件系统,简单理解就是多台机器组成的一个文 ...

  4. Cocos2d-x旧引擎文件夹结构

    转自:http://blog.csdn.net/lwuit/article/details/7870395 Cocos2d-x的文件夹结构例如以下: 文件夹的详细结构介绍例如以下: Box2D:物理引 ...

  5. C#如何设置窗体不能修改大小

    找到FormBorderStyle设置为FixedDialog即可                      

  6. Android怎样监听蓝牙耳机的按键事件

    Android怎样监听蓝牙耳机的按键事件 写在前面: 直接想要代码非常easy,你直接把滚动栏拉到最底端就能够看到.假设想要十分地了解为什么,那就依照我规划的一步一步来理解.下面測试环境以手头上有的「 ...

  7. IOS推断是否安装微信qq

    BOOL weicaht = [[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"mqqapi://" ...

  8. 《软件project》——编码

       编码的目的是使用选定的程序设计语言,把模块的过程描写叙述翻译为用该语言书写的源程序. 源程序应该正确可靠.简明清晰,并且具有较高的效率.在编程的步骤中,要把软件具体设计的表达式翻译成为编程语言的 ...

  9. python uzip

    import zipfile import osdef un_zip(file_name): """unzip zip file""" zi ...

  10. Oracle新建数据库

    确定楼主是以管理员身份登录的:1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password ...