1、项目结构
2、Employee.java
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 + "]"; } public Employee(Integer id, String lastName, String email, String gender) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; } public Employee() { super(); // TODO Auto-generated constructor stub } }
3、EmployeeMapperPlus.java
package com.atguigu.mybatis.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.MapKey; import com.atguigu.mybatis.bean.Employee; public interface EmployeeMapperPlus { public Employee getEmpById(Integer id); }
4、MybatisTest.java
package com.atguigu.mybatis.test; import java.io.IOException; import java.io.InputStream; 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 org.junit.Test; import com.atguigu.mybatis.bean.Employee; import com.atguigu.mybatis.dao.EmployeeMapperPlus; public class MybatisTest { private SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test05() throws IOException { SqlSessionFactory sqlSessionFactory =getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交 SqlSession openSession= sqlSessionFactory.openSession(); try { /*2.获取接口的实现对象*/ EmployeeMapperPlus mapper= openSession.getMapper(EmployeeMapperPlus.class); Employee employee=mapper.getEmpById(1); System.out.println(employee); //3、手动提交 openSession.commit(); } finally { openSession.close(); } } }
5、EmployeeMapperPlus.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"> <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapperPlus"> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp"> <!-- 指定主键的封装规则 id可以放在result标签内,自定义主键对底层有优化 column:指定哪一行 property:指定对应的javabean属性 --> <id column="id" property="id"/> <!-- 定义普通列封装规则 ,其他不指定的列会自动封装--> <result column="last_name" property="lastName"/> </resultMap> <!-- public Employee getEmpById(Integer id); --> <select id="getEmpById" resultMap="MyEmp"> select * from tbl_employee where id=#{id} </select> </mapper>
①指定主键的封装规则
id可以放在result标签内,自定义主键对底层有优化
column:指定哪一行
property:指定对应的javabean属性
②定义普通列封装规则 ,其他不指定的列会自动封装
6、log4j.xml
<?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"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) " /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="debug" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
7、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"> <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="111111" /> </dataSource> </environment> </environments> <mappers> <!-- 注册userMapper.xml文件, resoure:引用类路径下的sql映射文件 mybatis/mapper/EmployeeMapper.xml url:引用网络路径或磁盘下的sql映射文件 file:///var/mappers/AuthorMapper.xml class:引用注册接口 ①、有sql配置文件:映射文件名必须和接口同名,并且放在与接口同一目录下 ②、没有sql映射文件:所有的sql都是利用注解写在接口上 ③、推荐:比较重要的,复杂的Dao接口,写sql映射文件,便于维护 不重要,简单的Dao接口为了开发快速可以写注解 package:用于有sql映射文件的接口注册 <package name="com.atguigu.mybatis.dao"/> --> <mapper resource="com/atguigu/mybatis/dao/EmployeeMapperPlus.xml"/> <!-- <package name="com.atguigu.mybatis.dao"/> --> </mappers> </configuration>