MyBatis入门 + 接口式编程(idea实现)
1、由于是入门级demo,所以直接弄个java工程,也不用maven了
先来看工程的目录结构
这里学习下:
java中build path是什么?
如果你bai只是将jar包复制到Java工程里面去,那么是无法使用到jar包里面zhi的类,因为编程ide无法找到jar包。所以build path其实就是添加jar包的路径,让ide可以找到这个路径对应的jar包的文件,从而来能够使用jar包中的方法。
如何将lib中的jar包build path + idea如何将一个目录变成资源文件夹自行百度
2、java bean
package com.atguigu.mybatis.bean; public class Employee { private Integer id; private String lastName; private String email; private String gender; 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 getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]"; } }
3、mybatis的全局配置文件 + sql映射文件+ log4j的配置文件
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="houchen" /> </dataSource> </environment> </environments> <!-- 将我们写好的sql映射文件(EmployeeMapper.xml) 一定要注册到全局配置文件(mybatis-config.xml)中 --> <mappers> <mapper resource="EmployeeMapper.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="com.atguigu.mybatis.mapper.EmployeeMapper"> <!-- namespace:名称空间;指定为接口的全类名 id:唯一标识 resultType:返回值类型 #{id}:从传递过来的参数中取出id值 public Employee getEmpById(Integer id); --> <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee"> select id,last_name lastName,email,gender from tbl_employee where id = #{id} </select> </mapper> =============================================================== <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> 4、mapper 接口 public interface EmployeeMapper { public Employee getEmpById(int id); }
4、mapper 接口
public interface EmployeeMapper { public Employee getEmpById(int id); }
5、测试代码
/* 1、sqlSession代表和数据库的一次会话,用完必须关闭 2、SqlSession是非线程安全的,每次使用都应该创建一个新的对象 3、mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象 4、两个重要的配置文件: mybatis的全局配置文件 sql映射文件 */ public class MybatisTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory; } /* * 1、根据xml配置文件(mybatis的全局配置文件)创建一个SqlSessionFactory对象 * 2、sql映射文件 * 3、将sql映射文件注册到mybatis全局配置文件 */ @Test public void test() throws IOException { SqlSession openSession = getSqlSessionFactory().openSession(); try { Employee employee = openSession.selectOne( "com.atguigu.mybatis.dao.EmployeeMapper.getEmpById", 1); System.out.println(employee); } finally { openSession.close(); } } @Test public void testMapper() throws IOException { SqlSession openSession = getSqlSessionFactory().openSession(); EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee e = mapper.getEmpById(1); System.out.println(e); } }