MyBatis练习——使用MyBatis查询所有职员信息
实现要求:
使用MyBatis查询所有职员信息
create table employee(
id int not null auto_increment,
name varchar(255) not null,
gender varchar(2) null,
position varchar(255) null, -- 岗位
nationality varchar(255) null, -- 国籍
primary key(id)
);
实现思路:
在上一个项目基础上,在src/main/resources下面创建MyBatis的配置文件mybatis-config.xml。
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>
<!-- 设置包的别名 -->
<typeAliases>
<package name="jack.mybatis.simple.model" />
</typeAliases>
<environments default="development">
<!--定义数据源,默认使用第一个 -->
<environment id="development">
<transactionManager type="JDBC">
<!-- 事务处理方式 -->
<property name="" value="" />
</transactionManager>
<!-- 每个environment元素定义的环境id -->
<dataSource type="UNPOOLED">
<!-- 数据源配置 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!--映射 -->
<mappers>
<mapper resource="jack/mybatis/simple/mapper/EmployeeMapper.xml" />
</mappers>
</configuration>
在src/main/java下创建一个基础的包jack.mybatis.simple,在这个包下面再创建model包。根据数据库表employee,在model包下创建实体类Employee。
Employee.java
package jack.mybatis.simple.model;
public class Employee {
private int id;
private String name;
private String gender;
private String position;
private String nationality;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Employee(int id, String name, String gender, String position, String nationality) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.position = position;
this.nationality = nationality;
}
public Employee() {
super();
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", position=" + position
+ ", nationality=" + nationality + "]";
}
}
在src/main/resources下面创建jack/mybatis/simple/mapper目录,再在该目录下面创建与实体类Employee对应的映射文件EmployeeMapper.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">
<!-- Employee类的映射 -->
<mapper namespace="jack.mybatis.simple.model">
<select id="selectAll" resultMap="Employee">
select * from employee
</select>
</mapper>
配置Log4j。
# 全局配置
log4j.rootLogger=ERROR, stdout
# MyBatis日志配直
log4j.logger.mapper=TRACE
# 控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
编写测试代码查询所有职员的信息。
package test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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.BeforeClass;
import org.junit.Test;
import jack.mybatis.simple.model.Employee;
public class EmployeeTest {
private static SqlSessionFactory sqlSessionFactory;
/* 完成mybatis配置的加载,创建得到SqlSessionFactory */
@BeforeClass
public static void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 获取SqlSession对象,用于调用方法得到数据 */
@Test
public void testSelectAll() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
List<Employee> selectList = sqlSession.selectList("selectAll");
for (Employee employee : selectList) {
System.out.println(employee.toString());
}
} finally {
// 不要忘记关闭sqlSession
sqlSession.close();
}
}
}
本文来自博客园,作者:明金同学,转载请注明原文链接:https://www.cnblogs.com/vmuu/p/15663324.html
公众号:【明金同学】