当前位置:   article > 正文

MyBatis练习——使用MyBatis查询所有职员信息_mybatis查询出所有的员工

mybatis查询出所有的员工

实现要求:

使用MyBatis查询所有职员信息

  1. create table employee(
  2. id int not null auto_increment,
  3. name varchar(255) not null,
  4. gender varchar(2) null,
  5. position varchar(255) null, -- 岗位
  6. nationality varchar(255) null, -- 国籍
  7. primary key(id)
  8. );

实现思路:

在上一个项目基础上,在src/main/resources下面创建MyBatis的配置文件mybatis-config.xml。

mybatis-config.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <!-- 设置包的别名 -->
  6. <typeAliases>
  7. <package name="jack.mybatis.simple.model" />
  8. </typeAliases>
  9. <environments default="development">
  10. <!--定义数据源,默认使用第一个 -->
  11. <environment id="development">
  12. <transactionManager type="JDBC">
  13. <!-- 事务处理方式 -->
  14. <property name="" value="" />
  15. </transactionManager>
  16. <!-- 每个environment元素定义的环境id -->
  17. <dataSource type="UNPOOLED">
  18. <!-- 数据源配置 -->
  19. <property name="driver" value="com.mysql.jdbc.Driver" />
  20. <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis" />
  21. <property name="username" value="root" />
  22. <property name="password" value="root" />
  23. </dataSource>
  24. </environment>
  25. </environments>
  26. <!--映射 -->
  27. <mappers>
  28. <mapper resource="jack/mybatis/simple/mapper/EmployeeMapper.xml" />
  29. </mappers>
  30. </configuration>

在src/main/java下创建一个基础的包jack.mybatis.simple,在这个包下面再创建model包。根据数据库表employee,在model包下创建实体类Employee。

Employee.java

  1. package jack.mybatis.simple.model;
  2. public class Employee {
  3. private int id;
  4. private String name;
  5. private String gender;
  6. private String position;
  7. private String nationality;
  8. public int getId() {
  9. return id;
  10. }
  11. public void setId(int id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getGender() {
  21. return gender;
  22. }
  23. public void setGender(String gender) {
  24. this.gender = gender;
  25. }
  26. public String getPosition() {
  27. return position;
  28. }
  29. public void setPosition(String position) {
  30. this.position = position;
  31. }
  32. public String getNationality() {
  33. return nationality;
  34. }
  35. public void setNationality(String nationality) {
  36. this.nationality = nationality;
  37. }
  38. public Employee(int id, String name, String gender, String position, String nationality) {
  39. super();
  40. this.id = id;
  41. this.name = name;
  42. this.gender = gender;
  43. this.position = position;
  44. this.nationality = nationality;
  45. }
  46. public Employee() {
  47. super();
  48. }
  49. @Override
  50. public String toString() {
  51. return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", position=" + position
  52. + ", nationality=" + nationality + "]";
  53. }
  54. }

在src/main/resources下面创建jack/mybatis/simple/mapper目录,再在该目录下面创建与实体类Employee对应的映射文件EmployeeMapper.xml文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <!-- Employee类的映射 -->
  5. <mapper namespace="jack.mybatis.simple.model">
  6. <select id="selectAll" resultMap="Employee">
  7. select * from employee
  8. </select>
  9. </mapper>

配置Log4j。

  1. # 全局配置
  2. log4j.rootLogger=ERROR, stdout
  3. # MyBatis日志配直
  4. log4j.logger.mapper=TRACE
  5. # 控制台输出配置
  6. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  7. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

编写测试代码查询所有职员的信息。

  1. package test;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.util.List;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. import jack.mybatis.simple.model.Employee;
  12. public class EmployeeTest {
  13. private static SqlSessionFactory sqlSessionFactory;
  14. /* 完成mybatis配置的加载,创建得到SqlSessionFactory */
  15. @BeforeClass
  16. public static void init() {
  17. try {
  18. Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
  19. sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  20. reader.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. /* 获取SqlSession对象,用于调用方法得到数据 */
  26. @Test
  27. public void testSelectAll() {
  28. SqlSession sqlSession = sqlSessionFactory.openSession();
  29. try {
  30. List<Employee> selectList = sqlSession.selectList("selectAll");
  31. for (Employee employee : selectList) {
  32. System.out.println(employee.toString());
  33. }
  34. } finally {
  35. // 不要忘记关闭sqlSession
  36. sqlSession.close();
  37. }
  38. }
  39. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/391771
推荐阅读
相关标签
  

闽ICP备14008679号