Mybatis实现部门表增删改查以及排序
废话不说,直接开门见山!
需要在WebContent下的lib下导入两个包
mybatis-3.2.5.jar
ojdbc6.jar
package com.xdl.entity;
import java.io.Serializable;
public class Dept implements Serializable{
private Integer deptno;//类型和名称与表保持一致
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Dept
package com.xdl.Mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.xdl.entity.Dept;
public interface DeptMapper {
/**
* 查询所有
*
*/
public List<Dept> findAll();
/**
* 通过id查询
*
*/
public Dept findById(int no);
/**
* 插入
*
*/
public int save(Dept dept);
/**
* 修改
*
*/
public int update(Dept dept);
/**
* 通过id删除
*
*/
public int delete(int no);
/**
* 排序
*
*/
public List<Dept> findAllOrder(@Param("n") String no);
}
DeptMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xdl.Mapper.DeptMapper">
<select id="findAll" resultType="com.xdl.entity.Dept">
select * from dept
</select>
<select id="findById" parameterType="int" resultType="com.xdl.entity.Dept">
select *
from dept where deptno = #{no}
</select>
<select id="findAllOrder" parameterType="String" resultType="com.xdl.entity.Dept">
select * from dept order by ${n}
</select>
<insert id="save" parameterType="com.xdl.entity.Dept">
insert into dept
(deptno,dname,loc) values (dept_seq.nextval,#{dname},#{loc})
</insert>
<update id="update">
update dept set dname = #{dname},loc = #{loc} where
deptno = #{deptno}
</update>
<delete id="delete">
delete from dept where deptno = #{no}
</delete>
</mapper>
deptmapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 将底层日志打印 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<!-- 指定数据库连 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="SCOTT" />
<property name="password" value="tiger" />
</dataSource>
</environment>
</environments>
<!-- 指定SQL定义文件 -->
<mappers>
<mapper resource="com/xdl/sql/DeptMapper.xml" />
</mappers>
</configuration>
sqlmap-config.xml
package com.xdl.test; import java.io.Reader; 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 MyBatisUtil {
static SqlSessionFactory factory;
static {
try {
String conf = "sqlmap-config.xml"; // 定义配置文件
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
Reader reader = Resources.getResourceAsReader(conf);
factory = builder.build(reader);
} catch (Exception e) {
e.printStackTrace();
}
} public static SqlSession getSession() {
SqlSession sqlSession = factory.openSession();
return sqlSession;
}
}
MybatisUtli
写一个测试类(实现查询和排序)
package com.xdl.test; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import com.xdl.Mapper.DeptMapper;
import com.xdl.entity.Dept; public class TestDeptMapper {
/**
* 查询所有
*/
@Test
public void testFindAll() {
SqlSession sqlSession = MyBatisUtil.getSession();
// sqlSession.getMapper(接口.class); 根据DeptMapper映射器接口动态生成实现对象
DeptMapper deptDao = sqlSession.getMapper(DeptMapper.class);
System.out.println(deptDao.getClass().getName());
List<Dept> list = deptDao.findAll();
for (Dept dept : list) {
System.out.println(dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
}
sqlSession.close();
} /**
* 进行排序
*/
@Test
public void testOrderBy() {
SqlSession sqlSession = MyBatisUtil.getSession();
// sqlSession.getMapper(接口.class); 根据DeptMapper映射器接口动态生成实现对象
DeptMapper deptDao = sqlSession.getMapper(DeptMapper.class);
System.out.println(deptDao.getClass().getName());
List<Dept> list = deptDao.findAllOrder("deptno");
for (Dept dept : list) {
System.out.println(dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
}
sqlSession.close();
}
}
TestDeptMapper
写一个测试类(实现增删改查排序)
package com.xdl.test; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import com.xdl.entity.Dept; public class TestDept {
private static SqlSession sqlSession = MyBatisUtil.getSession(); /**
* 查询所有
*/
@Test
public void testFindAll() {
// 使用sqlSession操作SQL selectList("id",parameterType值)
List<Dept> list = sqlSession.selectList("com.xdl.Mapper.DeptMapper.findAll");
for (Dept dept : list) {
System.out.println(dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
}
sqlSession.close();
} /**
* 根据ID查询
*/
@Test
public void testFindById() {
Dept dept = sqlSession.selectOne("com.xdl.Mapper.DeptMapper.findById", 10);
if (dept != null) {
System.out.println(dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
} else {
System.out.println("查询结果为空~~");
}
sqlSession.close();
} /**
* 插入
*/
@Test
public void testSave() {
Dept dept = new Dept();
dept.setDname("xian");
dept.setLoc("dayanta");
int rows = sqlSession.insert("com.xdl.Mapper.DeptMapper.save", dept);
String str = "条记录插入成功";
System.out.println(rows + str);
sqlSession.commit();
sqlSession.close();
} /**
* 修改
*/
@Test
public void testUpdate() {
Dept dept = new Dept();
dept.setDeptno(10);
dept.setDname("spring");
dept.setLoc("bj");
int rows = sqlSession.update("com.xdl.Mapper.DeptMapper.update", dept);
String str = "条记录修改成功";
System.out.println(rows + str);
sqlSession.commit();
sqlSession.close();
} /**
* 通过id删除
*/
@Test
public void testDelete() {
Dept dept = new Dept();
int rows = sqlSession.delete("com.xdl.Mapper.DeptMapper.delete", 1);
String str = "条记录删除成功";
System.out.println(rows + str);
sqlSession.commit();
sqlSession.close();
}
}
TestDept
1.在MyBatis中定义SQL语句时,如果SQL里有?号,就必须写parameterType=””参数是int就写对应的类型并且名字可以自定义
2. 在查询的时候最后会返回一个结果集对象,所以就必须在后面继续追加resultType=”包名.实体类名”
3 在执行DML的时候里面要执行多个参数的时候,可以选择集合或者对象,
parameterType=”包名.实体类名”.参数,类型和实体类要一致,参数不一致,可以通过给sql起别名解决,类型不一致就需要对框架里的部分参数进行转换
4 通过实现增删改查,发现DQL有resultType属性,DML都没有resultType属性 数据访问层(Dao)
5 mapper.xml映射器里的<mapper namespace=”包名.接口名”,才可以 达到Mapper.xml里的数据库代码映射到接口中
总结为:
a. Mapper接口中方法名和sql定义id值保持一致
b. Mapper接口中方法参数类型和sql定义中parameterType类型保持一致
c. Mapper接口中方法返回类型,多行查询返回List,单行查询返回对象类型和resultType保持一致DML返回类型为int或void
Mapper映射器规则
1 Mapper接口中方法名与SQL定义id值保持一致
2 Mapper接口中方法参数类型与SQL定义中parameterType类型保持一致
3 Mapper接口中方法返回类型,多行查询返回List、单行返回对象,类型与resultType保 持一致;增删改操作返回类型为int或void
4 SQL定义文件namespace需要指定为"包名.接口名"
参数映射中${}和#{}的区别
1 #{}参数映射机制采用的是PrepareStatement,将SQL和参数分开发送
2 ${}参数映射机制采用Statement,将SQL和参数拼一起发送执行
3 建议采用#{}方式,更安全
4 ${}适合用在字段名或表名位置;#{}适合用在字段值位置
Mybatis实现部门表增删改查以及排序的更多相关文章
- python-列表增删改查、排序、两个list合并、多维数组等
一.list列表 数组 列表类型:list 下标从0开始,0,1,2... 二.列表增加元素 stus.append() 在列表末尾增加一个元素: stus.insert(,) 在指定位置添加一个元 ...
- GZFramwork数据库层《四》单据主从表增删改查
同GZFramwork数据库层<三>普通主从表增删改查 不同之处在于:实例 修改为: 直接上效果: 本系列项目源码下载地址:https://github.com/GarsonZhang/G ...
- GZFramwork数据库层《三》普通主从表增删改查
运行结果: 使用代码生成器(GZCodeGenerate)生成tb_Cusomer和tb_CusomerDetail的Model 生成器源代码下载地址: https://github.com/Gars ...
- GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)
运行效果: 使用代码生成器(GZCodeGenerate)生成tb_EmpLeave的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCodeGe ...
- GZFramwork数据库层《一》普通表增删改查
运行结果: 使用代码生成器(GZCodeGenerate)生成tb_MyUser的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCode ...
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
- Vc数据库编程基础MySql数据库的表增删改查数据
Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...
- TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查、有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句。
TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查.有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句. select 'grant '|| PRIV ...
- SSH框架下的多表增删改查
下载地址:SSH框架下的多表增删改查 点击进入码云Git下载 点击进入CSDN下载 项目结构: 项目代码就不全部贴出来了,只贴下核心代码.需要项目的自己可以去下载. package com.atgui ...
随机推荐
- Vue.js-10:第十章 - 组件间的数据通信
一.前言 在上一章的学习中,我们继续学习了 Vue 中组件的相关知识,了解了在 Vue 中如何使用组件的 data.prop 选项.在之前的学习中有提到过,组件是 Vue 中的一个非常重要的概念,我们 ...
- 多线程学习系列二(使用System.Threading)
一.什么是System.Threading.Thread?如何使用System.Threading.Thread进行异步操作 System.Threading.Thread:操作系统实现线程并提供各种 ...
- python中的del
python中的del,只删除变量,不删除数据,具体表现为: a=1,c=a,del a,(c=1) 和 a = [1,2,3,4,5] b= a[0] del a[0] print a ([2,3, ...
- 配置rsync+inotify实时同步
与上一篇同步做 配置rsync+inotify实时同步 1:调整inotify内核参数 在linux内核中,默认的inotify机制提供三个调控参数:max_queue_events.max_user ...
- Github项目推荐-图神经网络(GNN)相关资源大列表
文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | AI研习社 作者|Zonghan Wu 这是一个与图神经网络相关的资源集合.相关资源浏览下方 ...
- Git:三、工作原理
首先,我们对工作区也就是文件夹中的文档进行修改. 然后,把修改并需要存档的文档用add命令放到暂存区,并且可以放很多文档. 最后,一个阶段的工作告一段落,使用commit命令把暂存区的内容一股脑存到G ...
- Elasticsearch安装配置
文档地址: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/setup.html 官方页面提供自0.9版本以来的说明文档,由于我 ...
- emacs 高亮
用途:让某个单词高亮显示 1,安装 m-x 回车,输入list-packages 在列表中找到highlight-symbol后,鼠标点击它,再点击安装 2,在.emacs中配置 ;;高亮 (requ ...
- 第二周Python学习笔记
分支结构: ① 单分支结构: 非常简单,if 条件语句,如果为true 则输出结果.否则不输出结果 ② 二分支结构: 条件结果为true则执行语句1,否则就执行语句2 If <条件>: ...
- Devops step by step
接着上次分享的devops历程[Followme Devops实践之路], 大家希望能够出一个step by step手册, 那今天我就来和手把手来一起搭建这么一套环境, 演示整个过程! 实验环境需要 ...