Emp.java
package com.rk.entity; public class Emp { private Integer id; private String name; private Double sal; public Emp(){} public Emp(Integer id, String name, Double sal) { this.id = id; this.name = name; this.sal = sal; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } }
mybatis.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"/> <!-- 设置类型别名 --> <typeAliases> <typeAlias type="com.rk.entity.Emp" alias="emp"/> </typeAliases> <!-- 设置一个默认的连接环境信息 --> <environments default="mysql_development"> <environment id="mysql_development"> <!-- mybatis使用jdbc事务管理方式 --> <transactionManager type="JDBC"/> <!-- mybatis使用连接池方式来获取连接 --> <dataSource type="POOLED"> <!-- 配置与数据库交互的4个必要属性 --> <property name="driver" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </dataSource> </environment> <!-- 连接环境信息,取一个任意唯一的名字 --> <environment id="oracle_development"> <!-- mybatis使用jdbc事务管理方式 --> <transactionManager type="jdbc"/> <!-- mybatis使用连接池方式来获取连接 --> <dataSource type="pooled"> <!-- 配置与数据库交互的4个必要属性 --> <property name="driver" value="${oracle.driver}"/> <property name="url" value="${oracle.url}"/> <property name="username" value="${oracle.username}"/> <property name="password" value="${oracle.password}"/> </dataSource> </environment> </environments> <!-- 加载映射文件--> <mappers> <mapper resource="com/rk/entity/EmpMapper.xml"/> </mappers> </configuration>
db.properties
mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://127.0.0.1:3306/testdb mysql.username=root mysql.password=root oracle.driver=oracle.jdbc.driver.OracleDriver oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl oracle.username=scott oracle.password=tiger
EmpMapper.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"> <!-- namespace属性是名称空间,必须唯一 --> <mapper namespace="com.rk.entity.Emp"> <!-- resultMap标签:映射实体与表 type属性:表示实体全路径名 id属性:为实体与表的映射取一个任意的唯一的名字 --> <resultMap type="emp" id="empMap"> <!-- id标签:映射主键属性 result标签:映射非主键属性 property属性:实体的属性名 column属性:表的字段名 --> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sal" column="sal"/> </resultMap> <!-- insert标签:要书写insert这么一个sql语句 id属性:为insert这么一个sql语句取一个任意唯一的名字 parameterType:要执行的dao中的方法的参数,如果是类的话,必须使用全路径类 --> <insert id="add" parameterType="emp"> insert into emp(id,name,sal) values(#{id},#{name},#{sal}) </insert> <select id="paginationAll" parameterType="map" resultType="emp"> select id,name,sal from emp limit #{pstart},#{psize} </select> <select id="pagination" parameterType="map" resultType="emp"> select id,name,sal from emp where name like #{pname} limit #{pstart},#{psize} </select> </mapper>
MyBatisUtils.java
package com.rk.utils; import java.io.IOException; import java.io.Reader; import java.sql.Connection; 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 MyBatisUtils { private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); private static SqlSessionFactory sqlSessionFactory; static{ try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } private MyBatisUtils() {} public static SqlSession getSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession == null){ sqlSession = sqlSessionFactory.openSession(); threadLocal.set(sqlSession); } return sqlSession; } public static void closeSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession != null){ sqlSession.close(); threadLocal.remove(); } } public static void main(String[] args) { Connection conn = MyBatisUtils.getSqlSession().getConnection(); System.out.println(conn!=null ? "连接成功" : "连接失败"); } }
EmpDao.java
package com.rk.dao; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import com.rk.entity.Emp; import com.rk.utils.MyBatisUtils; public class EmpDao { /** * 添加员工 */ public void add(Emp emp){ SqlSession sqlSession = null; try { sqlSession = MyBatisUtils.getSqlSession(); int i = sqlSession.insert(Emp.class.getName() + ".add", emp); System.out.println("本次操作影响"+i+"行数据"); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally{ MyBatisUtils.closeSqlSession(); } } /** * 分页查询所有员工 */ public List<Emp> paginationAll(int start,int size){ SqlSession sqlSession = null; List<Emp> list = null; try { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("pstart", start); map.put("psize", size); sqlSession = MyBatisUtils.getSqlSession(); list = sqlSession.selectList(Emp.class.getName() + ".paginationAll",map); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally{ MyBatisUtils.closeSqlSession(); } return list; } /** * 根据姓名,分页查询所有员工 */ public List<Emp> pagination(String name,int start,int size){ SqlSession sqlSession = null; List<Emp> list = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("pname", "%" + name + "%"); map.put("pstart", start); map.put("psize", size); sqlSession = MyBatisUtils.getSqlSession(); list = sqlSession.selectList(Emp.class.getName() + ".pagination",map); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally{ sqlSession.commit(); MyBatisUtils.closeSqlSession(); } return list; } public static void main(String[] args) { EmpDao dao = new EmpDao(); // for(int i=0;i<20;i++){ // Emp emp = new Emp(30+i, "Tom" + i, 200D+i); // dao.add(emp); // } //List<Emp> list = dao.paginationAll(0, 5); List<Emp> list = dao.pagination("Tom",0, 5); if(list != null && list.size()>0){ for(Emp emp : list){ System.out.println( emp.getName() + ":"+emp.getSal()); } } } }
时间: 2024-10-07 00:30:19