一个完整的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="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项目,包含增删改查的更多相关文章
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序
分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图. 1.首先建立一个数 ...
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- MyBatis -- 对表进行增删改查(基于注解的实现)
1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1 首先须要定义映射sql的 ...
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)
最近看老罗的视频,跟着完成了利用Java操作MySql数据库的一个框架类JdbcUtils.java,完成对数据库的增删改查.其中查询这块,包括普通的查询和利用反射完成的查询,主要包括以下几个函数接口 ...
- [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo
做一个tableView,包含增删改移动功能,并且修改值的时候,在按home键的时候会自动保存.如果可以的话使者保存自定义的类数组保存到plist中. 实现步骤: 1.创建一个SingleViewAp ...
- Mybatis的简单增删改查
刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...
- SpringMVC,MyBatis商品的增删改查
一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...
随机推荐
- codeforces Gym 100735 D、E、G、H、I
http://codeforces.com/gym/100735 D题 直接暴力枚举 感觉这道题数据有点问题 为什么要先排下序才能过?不懂.. #include <stdio.h> #in ...
- JSP中自动刷新
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/auto-refresh.html: 细想一个显示在线比赛分数.股市状态或当前交易额的网页.对于所有这种类 ...
- 具体解说Android图片下载框架UniversialImageLoader之内存缓存(三)
前面的两篇文章着重介绍的是磁盘缓存,这篇文章主要是解说一下内存缓存.对于内存缓存.也打算分两篇文章来进行解说.在这一篇文章中,我们主要是关注三个类, MemoryCache.BaseMemoryCac ...
- AE After Effect 渲染如何输出设置
各种输出设置值的对比情况. Microsoft Video1压缩方法情况(该模式下无法采用RGB+Alpha): 一 深度为"数千种颜色",缩放为1280×720(HDV/HDTV ...
- Android内存泄露之开篇
先来想这三个问题 内存泄露是怎么回事 内存会泄露的原因 避免内存泄露 1.内存泄露怎么回事 一个程序中,已经不须要使用某个对象,可是由于仍然有引用指向它垃圾回收器就无法回收它,当然该对象占用的内存就无 ...
- RHEL6.5上Oracle ACFS与Linux samba一起使用时遇到的bug
RHEL上的Oracle ACFS与linux samba一起使用时遇到的bug 一.环境介绍: cat /etc/issue的结果为: Red Hat Enterprise Linux Server ...
- 【剑指Offer学习】【面试题65:滑动窗体的最大值】
题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4 ...
- Vue2.0生命周期和钩子函数的一些理解
转自:https://segmentfault.com/a/1190000008010666 前言 在使用vue一个多礼拜后,感觉现在还停留在初级阶段,虽然知道怎么和后端做数据交互,但是对于mount ...
- android 特殊符号开头的联系人归并至“#”下
在PeopleActivity界面.联系人的显示位置是由其display name的第一个字符决定的. 数字开头的联系人会显示在"#"这个header下. 中英文联系人会显示在&q ...
- 对“使用MyEclipse,写的jsp代码因有汉字而无法保存”问题的解决
使用MyEclipse编辑jsp时.有时会出现"使用MyEclipse,写的jsp代码因有汉字而无法保存"的现象,怎样解决呢? Window-->Preferences--& ...