Mybatis入门配置 详解

目的:使用mybatis用id查询数据

第一步:导入相对应的jar包

Mybatis入门配置 详解

第二步:配置dtd约束(只有配置了约束才可以进行xml自动提示)

https://blog.csdn.net/a15920804969/article/details/79107852

第三步:准备好一张表,和相对应的pojo(domain)

3-1、user表

Mybatis入门配置 详解

3-2、pojo


  1. package com.ziyang.mybatis.pojo;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class User implements Serializable {
  5. /**
  6. *
  7. */
  8. private static final long serialVersionUID = 1L;
  9. private Integer id;
  10. private String username;
  11. private String sex;
  12. private Date birthday;
  13. private String address;
  14. public Integer getId() {
  15. return id;
  16. }
  17. public void setId(Integer id) {
  18. this.id = id;
  19. }
  20. public String getUsername() {
  21. return username;
  22. }
  23. public void setUsername(String username) {
  24. this.username = username;
  25. }
  26. public String getSex() {
  27. return sex;
  28. }
  29. public void setSex(String sex) {
  30. this.sex = sex;
  31. }
  32. public Date getBirthday() {
  33. return birthday;
  34. }
  35. public void setBirthday(Date birthday) {
  36. this.birthday = birthday;
  37. }
  38. public String getAddress() {
  39. return address;
  40. }
  41. public void setAddress(String address) {
  42. this.address = address;
  43. }
  44. @Override
  45. public String toString() {
  46. return "User [id=" + id + ", username=" + username + ", sex=" + sex
  47. + ", birthday=" + birthday + ", address=" + address + "]";
  48. }
  49. }

第四步:sqlmap的创建(也就是写sql语句的地方)


  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!-- 写sql语句的
  6. namespace 命名空间,这个mapper的名字
  7. -->
  8. <mapper namespace="test">
  9. <!--
  10. 通过id查询一个用户
  11. id:是待会我们调用那条sql的标识
  12. parameterType:传入的参数的类型 (支持基本数据类型简写)
  13. resultType:返回的数据类型
  14. #{xxx} : 相当于占位符 xxx随便写
  15. -->
  16. <select id="findUserById" parameterType="Integer" resultType="com.ziyang.mybatis.pojo.User">
  17. select * from user where id = #{a}
  18. </select>
  19. </mapper>

第五步:配置mybatis主配置文件 (sqlMapConfig.xml)


  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 和spring整合后 environments配置将废除 -->
  7. <environments default="development">
  8. <environment id="development">
  9. <!-- 使用jdbc事务管理 -->
  10. <transactionManager type="JDBC" />
  11. <!-- 数据库连接池 -->
  12. <dataSource type="POOLED">
  13. <property name="driver" value="com.mysql.jdbc.Driver" />
  14. <property name="url"
  15. value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
  16. <property name="username" value="root" />
  17. <property name="password" value="123" />
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <!-- Mapper文件的位置 -->
  22. <mappers>
  23. <mapper resource="com/ziyang/mybatis/sqlmap/User.xml"/>
  24. </mappers>
  25. </configuration>

第六步:测试:


  1. package com.ziyang.mybatis.test;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.Test;
  9. import com.ziyang.mybatis.pojo.User;
  10. public class Demo_1 {
  11. @Test
  12. public void test1() throws IOException {
  13. //加载核心配置文件
  14. InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
  15. //创建一个sqlSessionFactory
  16. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
  17. //创建sqlsession
  18. SqlSession sqlSession = sqlSessionFactory.openSession();
  19. //执行sql语句
  20. User user = sqlSession.selectOne("test.findUserById",1);
  21. System.out.println(user);
  22. }
  23. }

结构目录:

Mybatis入门配置 详解

相关推荐