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 ...
随机推荐
- python接口自动化(十八)--重定向(Location)(详解)
简介 在实际工作中,有些接口请求完以后会重定向到别的url,而你却需要重定向前的url.URL主要是针对虚拟空间而言,因为不是自己独立管理的服务器,所以无法正常进行常规的操作.但是自己又不希望通过主域 ...
- .NET Core + Ocelot + IdentityServer4 + Consul 基础架构实现
先决条件 关于 Ocelot 针对使用 .NET 开发微服务架构或者面向服务架构提供一个统一访问系统的组件. 参考 本文将使用 Ocelot 构建统一入口的 Gateway. 关于 IdentityS ...
- Java进阶篇设计模式之十 ---- 访问者模式和中介者模式
前言 在上一篇中我们学习了行为型模式的解释器模式(Interpreter Pattern)和迭代器模式(Iterator Pattern).本篇则来学习下行为型模式的两个模式,访问者模式(Visito ...
- 为什么Eureca Client要分成服务提供者和服务消费者呢?
[学习笔记]转载 6)为什么Eureca Client要分成服务提供者和服务消费者呢? 通 常来讲,服务提供方是重量的耗时的,所以可能在n台机器上.而服务消费方是轻量的,通过配置ribbon和@Loa ...
- 抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式概述 定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类 抽象工厂抽象工厂,顾名思义,就是比工厂模式更抽象的工厂模式.在工厂模式中,一个具体工厂只负责生产一个具体产品 ...
- CSS入门知识汇总
1.CSS认识 在谈论CSS的概念之前,我们先说一说web标准的目的——其在于创建一个统一的用于web表现层的技术标准,以便通过不同浏览器或终端设备向最终用户展示信息内容.一个网页的呈现是由三部分组成 ...
- openlayers4 入门开发系列之台风轨迹篇
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- Python算法练习--把搜索树转成双向链表
本文目前分享的题目都是来自于July的分享,然后把具体算法实现.搜索树转双向链表主要的实现逻辑是在中序遍历时,调整节点的左右子树:因为中序遍历是递归调用,所以在调整时一定要注意调整的位置,如果写错了, ...
- 使用清华开源镜像安装tensorflow
安装tensorflow时,如果使用直接安装速度相对较慢,采取清华大学的镜像会提高速度.GPU版本安装方法:pip install tensorflow-gpu==1.8 -i https://pyp ...
- Spring的历史及哲学
Spring的历史和哲学 1.Spring 历史 时间回到2002年,当时正是 Java EE 和 EJB 大行其道的时候,很多知名公司都是采用此技术方案进行项目开发.这时候有一个美国的小伙子认为 E ...