学习Mybatis这么多天,那么我给大家分享一下我的学习成果。从最基础的开始配置。
一、创建一个web项目,看一下项目架构
二、说道项目就会想到需要什么jar
三、就是准备大配置链接Orcl数据库
<?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> <!-- 别名的定制 --> <typeAliases> <!-- 按类型名定制别名 --> <!-- <typeAlias type="cn.happy.entity.Student" alias="Student"/> --> <!-- 拿当前指定包下的简单类名作为别名 --> <package name="cn.happy.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 使用jdbc的事务 --> <transactionManager type="JDBC" /> <!-- 使用自带的连接池 --> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="T2" /> <property name="password" value="T2" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/happy/dao/StudentDAO.xml" /> </mappers> </configuration>
四、准备小配置
<?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="cn.happy.dao.IStudentDAO">
//增删改 </mapper>
五、准备工具类
package cn.happy.util; import java.io.IOException; 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; /** * 工具类 * @author Happy * */ public class MybatisUtil { private static String config="mybatis-config.xml"; static Reader reader; static{ try { reader= Resources.getResourceAsReader(config); } catch (IOException e) { e.printStackTrace(); } } private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //提供一个可以获取到session的方法 public static SqlSession getSession() throws IOException{ System.out.println("22222"+factory); //弊病,就是工厂是 // 1.1 openSession到底做了什么 SqlSession session = factory.openSession(); System.out.println("3333"); return session; } }
六、准备实体类
package cn.happy.entity; import java.util.Date; /** * 学生实体类 * @author Happy * */ public class Student { private Integer stuno; private String stuname; private Integer stuage; private Date studate; public String toString() { return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage=" + stuage + ", studate=" + studate + "]"; } public Integer getStuno() { return stuno; } public void setStuno(Integer stuno) { this.stuno = stuno; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public Integer getStuage() { return stuage; } public void setStuage(Integer stuage) { this.stuage = stuage; } public Date getStudate() { return studate; } public void setStudate(Date studate) { this.studate = studate; } }
七、下面是具体的代码增删改查前面是公用的
(一)下面实现查看所有
1在接口定义方法
//查询所有记录 public List<Student> findAll() throws IOException;
2在配置中=======>由于动态加载所以不需要实现类
<!-- 查询所有 --> <select id="findAll" resultType="Student"> select * from student </select>
3测试类中
IStudentDAO dao; @Before public void initData() throws IOException{ SqlSession session = MybatisUtil.getSession(); //动态踢出实现类 //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO"> dao=session.getMapper(IStudentDAO.class); } /** * selectALl学生 * @throws IOException */ @Test public void findAll() throws IOException{ List<Student> list = dao.findAll(); for (Student student : list) { System.out.println(student.getStuname()); } }
(二)下面是添加
1在接口定义方法
//添加 public int addStu(Student stu) throws IOException;
2在配置中=======>由于动态加载所以不需要实现类
<insert id="insertStudent" parameterType="Student" > insert into student(stuname,stuage,studate) values(#{stuname},#{stuage},#{studate}) <!-- sqlserver 和Mysql 只有自自增 自增时机和insert时机: 先insert返回自增值 Oracle先产生一个自增值,然后再执行insert --> <selectKey keyProperty="stuno" resultType="int"> <!-- Mysql的 --> select @@identity <!-- orcl的 --> select sql_num.currval from dual </selectKey> </insert>
3测试类中
@Test public void testAdd() throws IOException{ Student stu=new Student(); stu.setStuname("Y2161好人"); stu.setStuage(21); stu.setStudate(new Date()); System.out.println("添加前======="+stu); IStudentDAO dao=new StudentDAOImpl(); dao.addStu(stu); System.out.println("添加后======="+stu); }
(二)下面是删除
1在接口定义方法
//删除 public int delStu(int id) throws IOException;
2在配置中=======>由于动态加载所以不需要实现类
<!--删除学生 --> <delete id="delStudent"> delete from student where stuno=#{xxx}<!-- #{xxx}随便写,起到一个占位的作用 --> </delete>
3测试类中
/** * 删除学生 * @throws IOException */ @Test public void delStudent() throws IOException{ dao.delStu(2); System.out.println("ok"); }
(二)下面是模糊查询
1在接口定义方法
//按照学生姓名查询学生集合(一个是实体一个是字符串其实都可以) public List<Student> findStudntByName(Student stu) throws IOException; public List<Student> findStudntByName(String stuname) throws IOException;
2在配置中=======>由于动态加载所以不需要实现类
<!--模糊查询 mysql的数据库 -->
<select id="findStudentByName" resultType="Student">
<!-- select * from student where stuname like concat('%',#{stuname},'%') -->
select * from student where stuname like '%${value}%'
</select>
3测试类中
/** * 模糊查询 * @throws IOException */ @Test public void findStudentByName() throws IOException{ /*Student stu=new Student(); stu.setStuname("人");*/ List<Student> list = dao.findStudntByName("人"); for (Student student : list) { System.out.println(student.getStuname()); } }