一个完整的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 ...
随机推荐
- Codeforces704B. Ant Man
n<=5000个数轴上的点,有属性x,a,b,c,d,从i跳到j的代价如下: 问从s跳到t的最小代价. 方法?:先构造s->t链,然后依次插入其他点,每次选个最佳的位置.过了这题,正确性不 ...
- 图解Windows下安装WebLogic
Oracle 的Weblogic分开发者版本和生产版本,有32位和64位.一般生产版本的weblogic是64位的,安装文件是一个大小为1G多的jar包.去oracle官网上下载64版weblogic ...
- Windows 7 SID 修改
在安裝Windows系統時會產生一個獨一無二的SID (Security ID),它用來識別每一部主機,若在同一個區域網路內有兩部相同SID的主機,會出現警告訊息.一般而言,每次安裝時的SID不可能會 ...
- TextView设置成仅仅读
TextView设置成仅仅读 方法一:代理 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { return NO; } 方法二:设 ...
- 获取路由事件的源Source和OriginalSource
路由事件的消息包括在RoutedEventArgs实例中,该实例有两个属性Source和OriginalSource,都是表示路由事件传递的起点.即事件消息的源头.仅仅只是Source表示的是Logi ...
- 【Unix编程】进程间通信(IPC)
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息.IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket. ...
- 6个变态的C语言Hello World程序 之 雷人的程序语言
以下的六个程序片段主要完毕这些事情: 输出Hello, World 混乱C语言的源码 以下的全部程序都能够在GCC下编译通过,仅仅有最后一个须要动用C++的编译器g++才干编程通过. hello1.c ...
- (四)Java 基础语法
Java 基础语法 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一 ...
- Codeforces Round #319 (Div. 2)B. Modulo Sum DP
B. Modulo Sum ...
- b-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='") SELECT id FROM qqzoneshuoshuo WHERE words=
db-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci, ...