一、创建全局文件

<?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> <!--默认的数据库配置 修改成mysql 之前是 development-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/qy100?CharacterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <!--mybatis 是使用
1.简单的xml 或者
2.注解来配置和映射原生类型,接口和java的pojo(普通老式java对象)-->
<!--为数据库中记录的-->
<!--这里就是将在dao层下创建的DeptMapper.xml映射文件 加入到全局文件中 -->
<mappers>
<mapper resource="com/aaa/dao/DeptMapper.xml"/>
</mappers> </configuration>

二、创建实体类

package com.aaa.entity;

public class Dept {
private int id;
private String ename;
private String job;
private double sal; public Dept(int id, String ename, String job, double sal) {
this.id = id;
this.ename = ename;
this.job = job;
this.sal = sal;
} public Dept(int id, String ename) {
this.id = id;
this.ename = ename;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} 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 double getSal() {
return sal;
} public void setSal(double sal) {
this.sal = sal;
} @Override
public String toString() {
return "Dept{" +
"id=" + id +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", sal=" + sal +
'}';
}
}

三、在DAO层中创建  mybatis-mapper.xml 映射文件。

<?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"> <!--mybatis的原生接口。--> <!--工作空间就是
1.我在dao层下 创建的DeptMapper 文件
2.或者是对应的 dao 层中的接口。
-->
<mapper namespace="com.aaa.dao.DeptMapper">
<select id="selectDept" resultType="com.aaa.entity.Dept">
select * from dept;
</select> <delete id="deleteDept" parameterType="com.aaa.entity.Dept">
delete from dept where id=5;
</delete> <insert id="insertDept" parameterType="com.aaa.entity.Dept">
insert into dept (ename,job,sal) values("武王嬴荡","秦王",1200);
</insert> <update id="updateDept" parameterType="com.aaa.entity.Dept">
update dept set ename="嬴政" ,job="千古第一帝" where id=5;
</update> </mapper>

四、测试

package com.aaa.test;

import com.aaa.entity.Dept;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List; /* 测试 mybatis原生的接口
* 1.Resources:资源类
* 2.SqlSessionFactoryBuilder:构建器
* 3.SqlSessionFactory :会话工厂
*
* */
public class Test {
public static void main(String[] args) throws IOException {
//1.加载全局文件和配置文件 String resource="mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is); //2.开启会话
SqlSession sqlSession = ssf.openSession();
List<Dept> deptList = sqlSession.selectList("com.aaa.dao.DeptMapper.selectDept"); for (Dept dept:deptList
) {
System.out.println("部门信息"+dept);
}
//2.关闭会话
sqlSession.close();
}
}

五、封装一个单列的会话工厂。

package com.aaa.util;

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 java.io.IOException;
import java.io.InputStream;
// 懒汉模式 节省资源
public class Factory {
private static SqlSessionFactory ssf=null; static{
String resource="mybatis-config.xml";
InputStream is=null; try {
is = Resources.getResourceAsStream(resource);
ssf = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSessionFactory getSsf(){
return ssf;
}
public static SqlSession getSqlSession(){
return ssf.openSession();
}
}

六、测试

package com.aaa.test;

import com.aaa.entity.Dept;
import com.aaa.util.Factory;
import org.apache.ibatis.session.SqlSession; import java.util.List;
/*
* 1.增删改查 注意!执行增删改的操作时 查询语句只能放在后面执行 否则报错!
* 2.进行增删改操作 需要进行提交!
*
* */
public class Test02 {
public static void main(String[] args) {
//利用单列的工厂类 获得连接
SqlSession sqlSession = Factory.getSqlSession(); //1.增加
sqlSession.insert("com.aaa.dao.DeptMapper.insertDept");
sqlSession.commit();
//2.修改
sqlSession.update("com.aaa.dao.DeptMapper.updateDept");
sqlSession.commit();
//3.删除
sqlSession.delete("com.aaa.dao.DeptMapper.deleteDept");
sqlSession.commit(); //4.查询
List<Dept> deptList = sqlSession.selectList("com.aaa.dao.DeptMapper.selectDept"); for (Dept dept:deptList
) {
System.out.println("部门信息"+dept);
}
//关闭会话
sqlSession.close();
}
}

最新文章

  1. [转载]大型网站应用中 MySQL 的架构演变史
  2. Vertical Menu ver4
  3. 内存中OLTP与内存不足
  4. 我的c++学习(12)指针作为函数参数
  5. 2015 ACM Syrian Collegiate Programming Contest
  6. Android 利用Service实现下载网络图片至sdk卡
  7. std::cout彩色输出
  8. 【优先队列】HDU 1873——看病找医生
  9. UVA 185(暴力DFS)
  10. SQL Server调优系列进阶篇 - 查询优化器的运行方式
  11. (转)dedecms代码详解 很全面
  12. mysql 插入Emoji表情报错
  13. ElasticSearch(2)-文档
  14. 11-散列4 Hashing - Hard Version
  15. 【Java入门提高篇】Day11 Java代理——JDK动态代理
  16. 事务,Oracle,MySQL及Spring事务隔离级别
  17. SpringCloud(3)服务消费者(Feign)
  18. 2018-2019-2 20165312《网络攻防技术》Exp5 MSF基础应用
  19. vs code 前端如何以服务器模式打开 [安装服务器] server insteall
  20. C#异常--System.IO.FileLoadException:“混合模式程序集是针对“v2.0.50727”版的运行时生成的错误

热门文章

  1. 【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)
  2. 1276 - Very Lucky Numbers
  3. element ui el-tree回显问题及提交问题(当后台返回的el-tree相关数组的时候,子菜单未全部选中,但是只要父级菜单的id在数组中,那么他的子菜单为全部选中状态)
  4. [数据结构]严蔚敏版(C数据结构)配套实现程序111例
  5. 编写Java程序,车站只剩 50 张从武汉到北京的车票,现有 3 个窗口售卖,用程序模拟售票的过程,使用Runnable解决线程安全问题
  6. 编写Java程序,在子类老虎中重写父类动物的吃食方法
  7. PostgresSQL客户端pgAdmin4使用
  8. .net core中EFCore发出警告:More than twenty &#39;IServiceProvider&#39; instances have been created for internal use by Entity Framework
  9. ApkToolBoxGUI 0.0.8发布了!!
  10. Dubbo服务 上传文件解决方案以及Hessian协议