mybatis 原生写法创建项目
一、创建全局文件
<?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(); } }