package com.atgui.spring.mybatis.entities;
public class Employee {
private Integer id;
private String lastName;
private String email;
private double salary;
private Integer deptId;
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", salary=" + salary + ", deptId="
+ deptId + "]";
}
}
全局配置
<?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>
<!-- 1、properties导入外部属性文件
-resource:引入类路径下的属性文件
-url:引入网络上或者磁盘上的属性文件
-->
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.jdbcUrl}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers>
</configuration>
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">
<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>
两种方式实现:
@Test
public void test() throws IOException {
//1.创建SQLSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSessionFactory();
//回去sqlsession ,相当于jdbc中的connection
SqlSession session = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employeeById = mapper.getEmployeeById(01);
System.out.println(employeeById);
} finally {
session.close();
}
}
@Test
public void test2() throws IOException {
//1.创建SQLSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSessionFactory();
//回去sqlsession ,相当于jdbc中的connection
SqlSession session = sqlSessionFactory.openSession();
try {
Object selectOne = session.selectOne("getEmployeeById2","a");
System.out.println(selectOne);
} finally {
session.close();
}
}
package com.atgui.spring.mybatis.mapper;
import com.atgui.spring.mybatis.entities.Employee;
public interface EmployeeMapper {
public Employee getEmployeeById(Integer id);
}
<?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">
<!--
id:方法名字
resultType:返回值类型的全类名
-->
<mapper namespace="com.atgui.spring.mybatis.mapper.EmployeeMapper">
<select id="getEmployeeById" resultType="com.atgui.spring.mybatis.entities.Employee">
select id,last_name lastName,email,salary,dept_id deptId from employees where id = #{id}
</select>
</mapper>
db.properties
jdbc.username=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///mybatis
jdbc.initPoolSize=5
jdbc.minPoolSize=2
jdbc.maxPoolSize=10