一.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"> <!-- 使用propertys 引入外部文件 source引入类文件 URL 网络或磁盘 <setings >标签 --> <configuration> <properties resource="conf.properties"></properties> <settings> <!-- 开启驼峰命名 A_Name映射为aName --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <!-- 别名处理器 --> <typeAliases> <!-- 默认命名为类名小写 employee --> <typeAlias type="com.cws.mybatis.bean.Employee" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 注册配置文件 --> <mappers> <mapper resource="EmployeeMapper.xml" /> </mappers> </configuration>
二.EmployeeMapper.xml
配置文件 负责处理sql语句和编写
<?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="com.cws.mybatis.dao.EmployMapper"> <!-- namespace 名称空间 id 唯一标示 re...Type 返回值类型 #{id}从参数取出id 使用接口 id为方法 开启驼峰命名后 --> <!-- <select id="getEmpbyId" resultType="com.cws.mybatis.bean.Employee"> select id,last_name lastName,gender,email from tbl_emp where id = #{id} </select> --> <!-- <select id="getEmpbyId" resultType="com.cws.mybatis.bean.Employee"> 不适用别名 --> <!-- 使用type处理器 --> <select id="getEmpbyId" resultType="employee"> select * from tbl_emp where id = #{id} </select> </mapper>三.Employee.java bean类
package com.cws.mybatis.bean; public class Employee { Integer id; String lastName; String gender; String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]"; } }
四.接口,负责代理处理SQL方法
package com.cws.mybatis.dao; import com.cws.mybatis.bean.Employee; public interface EmployMapper { public Employee getEmpbyId(Integer id); }
五.关联配置文件,实现接口类
//关联配置文件 public SqlSessionFactory getSqlSessionFactory()throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder(). build(inputStream); return sqlSessionFactory; } /* @Test public void test() throws IOException { SqlSession opsession=getSqlSessionFactory().openSession(); try { Employee employee= opsession.selectOne("selectEmp", 1); System.out.println(employee); } finally { opsession.close();// TODO: handle exception } } */ @Test public void text01() throws IOException{ SqlSessionFactory sqlSessionFactory=getSqlSessionFactory(); SqlSession openSession=sqlSessionFactory.openSession(); try { //实现接口类 EmployMapper mapper=openSession.getMapper(EmployMapper.class); //实现接口方法 Employee employee=mapper.getEmpbyId(1); System.out.println(employee); } finally { //关闭 openSession.close(); } }