MyBatis框架(二)
导包,
配置mybatis的总配置文件: mybatis-config.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>
<!-- 引入数据库的信息的属性文件 -->
<properties resource="db.properties"></properties>
<typeAliases>
<package name="model"/>
</typeAliases>
<environments default="hanqi">
<environment id="hanqi">
<!--
JDBC:
MANAGED:托管
-->
<transactionManager type="JDBC" />
<!--
配置数据库源
POOLED: 连接池
UNPOOLED: 非连接池
JNDI: 使用应用服务器上的数据库连接
-->
<dataSource type="POOLED">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driver" value="${jdbc.driver}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
<mapper resource="mapper/StudentMapper.xml" />
<mapper class=""></mapper>
<mapper url="f:/test/StudentMapper.xml"></mapper>
-->
<!--
如果有多个,用包名,自动在包下面搜索
-->
<package name="com.hanqi.maya.mapper" />
</mappers>
</configuration>
新建每个实体类的接口和映射文件,并在xml映射文件中引入接口
package mapper;
import java.util.List;
import java.util.Map;
import model.Student;
public interface StudentMapper {
public List<Student> selectAllStudent();
public int insertStudent(Student s);
public int updateStudent(Map m);
public int deleteStudent(Map m);
}
<?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="mapper.StudentMapper">
<select id="selectAllStudent" resultType="Student">
select * from student
</select>
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sno" keyColumn="SNO">
insert into student values(test1.nextval, #{name}, #{sex}, #{sbirthday}, #{sclass})
</insert>
<update id="updateStudent" parameterType="Map">
update student s set s.sname=#{stuname},s.sbirthday=#{newDate}
where s.sno=#{stusno}
</update>
<delete id="deleteStudent" parameterType="Map">
delete student s where s.sno=#{stusno}
</delete>
</mapper>
namespace中如果是ID,可以随便写,需要唯一,如果是引入接口,需要接口和本文件名一致。
测试:
package test;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import mapper.StudentMapper;
import model.Student;
import util.MyBatisUtil;
public class JUTest {
private SqlSession ss;
//定义接口类,拿到接口
private StudentMapper sm;
@Before
public void setUp() throws Exception {
ss=MyBatisUtil.getSession();
sm=ss.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
MyBatisUtil.destory(ss);
}
@Test
public void test() {
List<Student> slist=sm.selectAllStudent();
/*Student s=new Student(null,"name","sex",new Date(),95053);
int a=sm.insertStudent(s);
System.out.println(a);*/
/*Map m=new HashMap();
m.put("stusno", "101");
m.put("stuname", "修改1");
m.put("newDate",new Date());
int a=sm.updateStudent(m);
System.out.println(a);*/
Map m1=new HashMap();
m1.put("stusno", "s");
int b=sm.deleteStudent(m1);
for(Student s:slist){
System.out.println(s);
}
}
}
关联查询:
一对一查询(三种方式),
一对多查询
部门表P_Dept和员工表P_Emp
员工表中有部门编号
员工类中有部门属性
部门类中有员工集合属性
通过联合查询,查询员工时得到员工属性和部门,查询部门时通过联合查询得出员工列表
结构:

model包:
packa1ge model;
import java.util.Date;
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;
private int sex;
private Dept dept;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno, int sex,
Dept dept) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
this.sex = sex;
this.dept = dept;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + ", sex=" + sex + ", dept=" + dept + "]";
}
}
package model;
import java.util.List;
public class Dept {
private int deptno;
private String dname;
private String loc;
private List<Emp> elist;
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Dept(int deptno, String dname, String loc, List<Emp> elist) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
this.elist = elist;
}
public List<Emp> getElist() {
return elist;
}
public void setElist(List<Emp> elist) {
this.elist = elist;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int 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;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", elist=" + elist + "]";
}
}
mapper包:
package mapper;
import java.util.List;
import model.Dept;
import model.Emp;
public interface DeptMapper {
Dept selectDeptByDeptno(Integer id);
}
<?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="mapper.DeptMapper">
<resultMap type="Dept" id="abc">
<collection property="elist" column="deptno"
select="mapper.EmpMapper.selectEmpByDeptno" />
</resultMap>
<select id="selectDeptByDeptno" parameterType="Integer" resultMap="abc">
select * from p_dept d where d.deptno=#{deptno}
</select>
</mapper>
package mapper;
import java.util.List;
import model.Emp;
public interface EmpMapper {
//查询所有信息
List<Emp> selectAllEmp();
// 根据部门id查询员工
List<Emp> selectEmpByDeptno(Integer deptno);
}
<?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="mapper.EmpMapper">
<!-- 对象及联 -->
<resultMap type="Emp" id="empList">
<id property="empno" column="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="hiredate" column="hiredate" />
<result property="sal" column="sal" />
<result property="comm" column="comm" />
<result property="deptno" column="deptno" />
<result property="sex" column="sex"/>
<result property="dept.deptno" column="deptno"/>
<result property="dept.dname" column="dname"/>
<result property="dept.loc" column="loc"/>
</resultMap>
<select id="selectAllEmp" resultMap="empList">
select * from p_Emp e left join p_Dept d on d.deptno=e.deptno
</select>
<!-- 关联 -->
<!-- <resultMap type="Emp" id="empList1">
<id property="empno" column="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="hiredate" column="hiredate" />
<result property="sal" column="sal" />
<result property="comm" column="comm" />
<result property="deptno" column="deptno" />
<result property="sex" column="sex"/>
<association property="dept" resultMap="deptlist"></association>
</resultMap>
<resultMap type="Dept" id="deptlist">
<result property="deptno" column="deptno"/>
<result property="dname" column="dname"/>
<result property="loc" column="loc"/>
</resultMap>
<select id="selectAllEmp" resultMap="empList1">
select * from p_Emp e left join p_Dept d on d.deptno=e.deptno
</select> -->
<!-- 关联查询 -->
<!-- <resultMap type="Emp" id="empList3">
<association property="dept" column="deptno"
select="mapper.DeptMapper.selectDeptById" />
</resultMap>
<select id="selectAllEmp" resultMap="empList3">
select * from p_EMP t
</select> -->
<!-- 一对多 -->
<select id="selectEmpByDeptno" parameterType="Integer" resultType="Emp">
SELECT * FROM p_emp e WHERE e.deptno=#{deptno}
</select>
</mapper>
测试:
package test;
import static org.junit.Assert.*;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import mapper.DeptMapper;
import mapper.EmpMapper;
import model.Dept;
import model.Emp;
import util.MyBatisUtil;
public class JUTest {
private SqlSession ss;
private EmpMapper em;
private DeptMapper dm;
@Before
public void setUp() throws Exception {
ss = MyBatisUtil.getSession();
em = ss.getMapper(EmpMapper.class);
dm = ss.getMapper(DeptMapper.class);
}
@After
public void tearDown() throws Exception {
MyBatisUtil.destory(ss);
}
@Test
public void test() {
/*List<Emp> slist=em.selectAllEmp();
for(Emp e:slist){
System.out.println(e);
}*/
/*List<Emp> elist = em.selectEmpByDeptno(10);
System.out.println(elist);*/
Dept d = dm.selectDeptByDeptno(10);
System.out.println(d);
}
}
MyBatis框架(二)的更多相关文章
- Mybatis框架二:增删改查
这里是搭建框架和准备数据: http://www.cnblogs.com/xuyiqing/p/8600888.html 实现增删改查功能: 测试类: package junit; import ja ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
- OSGI企业应用开发(九)整合Spring和Mybatis框架(二)
上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...
- (转)MyBatis框架的学习(二)——MyBatis架构与入门
http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ...
- java实现微信支付宝等多个支付平台合一的二维码支付(maven+spring springmvc mybatis框架)
首先申明,本人实现微信支付宝等支付平台合多为一的二维码支付,并且实现有效时间内支付有效,本人采用的框架是spring springmvc mybatis 框架,maven管理.其实如果支付,不需要my ...
- Java框架之MyBatis框架(二)
Mybatis框架是相对于优化dao层的框架,其有效的减少了频繁的连接数据库(在配置文件xml中进行配置),将sql语句与java代码进行分离(写在XXXXmapper.xml文件中,一个表对应一个x ...
- JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- 【mybatis annotation】数据层框架应用--Mybatis(二) 基于注解实现数据的CRUD
使用MyBatis框架进行持久层开发 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBa ...
- SpringMVC框架二:SpringMVC与MyBatis整合
下面整合SpringMVC和MyBatis框架,并做一个小案例 创建数据库springmvc,并创建两张表,加入一些数据: 两张表:商品表,用户表 CREATE DATABASE springmvc; ...
随机推荐
- 开机后发现Win7桌面上什么都没有该如何恢复
开机后发现Win7桌面上什么都没有该如何恢复 win7桌面上什么都没有该如何恢复:当我们打开电脑,发现win7桌面上什么都没有,那么该如何恢复呢?下面由我来介绍windows7桌面上图标不显示的解决方 ...
- python版mapreduce题目实现寻找共同好友
看到一篇不知道是好好玩还是好玩玩童鞋的博客,发现一道好玩的mapreduce题目,地址http://www.cnblogs.com/songhaowan/p/7239578.html 如图 由于自己太 ...
- codeforces 466d Increase Sequence
D. Increase Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- linux shell 之终端读写文件数据流和重定向>,<,<<,>>
终端实现文件中数据流的读写: 重定向命令列表如下: 命令 说明 command > file 将输出重定向到 file.将终端数据写到文件file中 command < file 将输入重 ...
- Java对象的创建
学了很久的java,是时候来一波深入思考了.比如:对象是如何在JVM中创建,并且被使用的.本文主要讲解下new对象的创建过程.要想更深入的了解建议去认认真真的看几遍<深入理解Java虚拟机> ...
- PS软件操作应用—文字特效
前 言 JRedu 在图像调整和文字工具的分享文章中,对文字工具做了简单的介绍,包括了文字的字体.字号大小.颜色以及字间距行距等等的设置和修改,都是一些基本的功能,在这次的分享中我们介绍下文字特 ...
- Windows下Docker承载ASP.NET Core 应用
基本配置: Win7 64系统,Docker Toolbox, 主要步骤: [1]发布ASP.NET Core MVC应用,CD到项目根目录,执行dontnet publish [2]新建一个Dock ...
- js 中采用词法作用域
所谓的 词法( 代码 )作用域, 就是代码在编写过程中体现出来的作用范围. 代码一旦写好, 不用执行, 作用范围就已经确定好了. 这个就是所谓词法作用域. 在 js 中词法作用域规则: 1.函数允许访 ...
- c# DateTime 类
获得当前系统时间: DateTime dt = DateTime.Now;Environment.TickCount可以得到"系统启动到现在"的毫秒值DateTime now = ...
- [Vue安装教程]十分钟学会vue 安装
Vue的安装主要有一下几个步骤: 1.安装npm淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 2.安装脚手架工 ...