MyBatis(二):我的第一个MyBatis程序

嚴華  金牌会员 | 2022-9-17 08:39:48 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 821|帖子 821|积分 2463

在MyBatis的第一篇的学习中我们了解了MyBatis是什么和为什么学习MyBatis,本次我们将学习MyBatis的具体使用。
 思路:环境搭建——>MyBatis导入——>代码编写——>测试。
一、环境搭建
1.建立数据库
  1. CREATE DATABASE `MyBaties`;
  2. CREATE TABLE `user`(
  3.     `id` INT(20) NOT NULL PRIMARY KEY,
  4.     `username` VARCHAR(30) NOT NULL,
  5.     `password` VARCHAR(30) NOT NULL
  6. );
  7. INSERT INTO `user` (`id`,`username`,`password`)
  8. VALUES
  9. (10000,'jms1','123456'),
  10. (10001,'jms2','123456'),
  11. (10003,'jms3','123456')
复制代码
 2.建立一个maven项目
这个项目作为父项目。
修改pom.xml导入需要的依赖:mysql驱动、mydatis、junit。
  1.     <dependencies>
  2.       
  3.   <mappers>
  4.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  5.   </mappers>  
  6.   <mappers>
  7.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  8.   </mappers>  
  9.   <mappers>
  10.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  11.   </mappers><dependency>
  12.   <mappers>
  13.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  14.   </mappers>    <groupId>mysql</groupId>
  15.   <mappers>
  16.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  17.   </mappers>    <artifactId>mysql-connector-java</artifactId>
  18.   <mappers>
  19.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  20.   </mappers>    <version>8.0.30</version>
  21.   <mappers>
  22.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  23.   </mappers></dependency>
  24.   <mappers>
  25.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  26.   </mappers>
  27.   <mappers>
  28.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  29.   </mappers>
  30.   <mappers>
  31.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  32.   </mappers><dependency>
  33.   <mappers>
  34.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  35.   </mappers>    <groupId>org.mybatis</groupId>
  36.   <mappers>
  37.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  38.   </mappers>    <artifactId>mybatis</artifactId>
  39.   <mappers>
  40.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  41.   </mappers>    <version>3.5.10</version>
  42.   <mappers>
  43.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  44.   </mappers></dependency>
  45.   <mappers>
  46.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  47.   </mappers>
  48.   <mappers>
  49.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  50.   </mappers>
  51.   <mappers>
  52.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  53.   </mappers><dependency>
  54.   <mappers>
  55.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  56.   </mappers>    <groupId>junit</groupId>
  57.   <mappers>
  58.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  59.   </mappers>    <artifactId>junit</artifactId>
  60.   <mappers>
  61.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  62.   </mappers>    <version>4.13.2</version>
  63.   <mappers>
  64.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  65.   </mappers>    <scope>test</scope>
  66.   <mappers>
  67.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  68.   </mappers></dependency>
  69.      </dependencies>
复制代码
 3.建立一个子模块
(1)编写mybatis的核心配置文件mybatis-config.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.   <environments default="development">
  7.     <environment id="development">
  8.       <transactionManager type="JDBC"/>
  9.       <dataSource type="POOLED">
  10.   <mappers>
  11.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  12.   </mappers><property name="driver" value="${driver}"/>
  13.   <mappers>
  14.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  15.   </mappers><property name="url" value="${url}"/>
  16.   <mappers>
  17.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  18.   </mappers><property name="username" value="${username}"/>
  19.   <mappers>
  20.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  21.   </mappers><property name="password" value="${password}"/>
  22.       </dataSource>
  23.     </environment>
  24.   </environments>
  25.   <mappers>
  26.     <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  27.   </mappers>
  28. </configuration>
复制代码
这是我个人的使用:
  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.   <environments default="development">
  7.     <environment id="development">
  8.       <transactionManager type="JDBC"/>
  9.       <dataSource type="POOLED">
  10.   <mappers>
  11.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  12.   </mappers><property name="driver" value="com.mysql.jdbc.Dirver"/>
  13.   <mappers>
  14.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  15.   </mappers><property name="url" value="jdbc:mysql://localhost:3306/mybaties?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
  16.   <mappers>
  17.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  18.   </mappers><property name="username" value="root"/>
  19.   <mappers>
  20.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  21.   </mappers><property name="password" value="123456"/>
  22.       </dataSource>
  23.     </environment>
  24.   </environments>
  25. </configuration>
复制代码
(2)编写mybatis工具类MyBatisUtil.class
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。
从 XML 文件中构建 SqlSessionFactory 的实例非常简单,建议使用类路径下的资源文件进行配置。 但也可以使用任意的输入流(InputStream)实例,比如用文件路径字符串或 file:// URL 构造的输入流。MyBatis 包含一个名叫 Resources 的工具类,它包含一些实用方法,使得从类路径或其它位置加载资源文件更加容易。
官方文档给出了以下三行代码:
  1. String resource = "org/mybatis/example/mybatis-config.xml";
  2. InputStream inputStream = Resources.getResourceAsStream(resource);
  3. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
复制代码
我们要做的就是把这三行代码封装到MyBatisUtil.class中:
  1. 1 package com.jms.utils;
  2. 2
  3. 3
  4. 4 import java.io.IOException;
  5. 5 import java.io.InputStream;
  6. 6
  7. 7 import org.apache.ibatis.io.Resources;
  8. 8 import org.apache.ibatis.session.SqlSession;
  9. 9 import org.apache.ibatis.session.SqlSessionFactory;
  10. 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  11. 11
  12. 12 //SqlSessionFactory-->SqlSession
  13. 13 public class MyBatisUtil {
  14. 14     
  15. 15     private static SqlSessionFactory sqlSessionFactory;
  16. 16     
  17. 17     //获取SqlSessionFactory对象
  18. 18     static {
  19. 19  <mappers>
  20.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  21.   </mappers> try {
  22. 20  <mappers>
  23.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  24.   </mappers>     String resource = "mybatis-config.xml";
  25. 21  <mappers>
  26.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  27.   </mappers>     InputStream inputStream = Resources.getResourceAsStream(resource);
  28. 22  <mappers>
  29.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  30.   </mappers>     sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);   
  31. 23  <mappers>
  32.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  33.   </mappers> } catch (IOException e) {
  34. 24  <mappers>
  35.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  36.   </mappers>     e.printStackTrace();
  37. 25  <mappers>
  38.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  39.   </mappers> }
  40. 26     }
  41. 27     
  42. 28     //通过SqlSessionFactory获取SqlSession对象,其中包含了面向数据库执行执行SQL命令所需要的方法
  43. 29     public static SqlSession getSqlSession() {
  44. 30  <mappers>
  45.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  46.   </mappers> return sqlSessionFactory.openSession();
  47. 31     }
  48. 32 }
复制代码
官网上也有着不进行核心配置,直接用纯代码进行编写的模式,本文就不做过多赘述,可自行了解。
3.编写代码
(1)首先我们要写一个实体类user.class,这个实体类应该与我们前面数据库建立的user表对应。
  1. 1 package com.jms.pojo;
  2. 2
  3. 3 public class user {
  4. 4     
  5. 5     private int id;
  6. 6     private String username;
  7. 7     private String password;
  8. 8     
  9. 9     public user() {
  10. 10  <mappers>
  11.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  12.   </mappers>
  13. 11     }
  14. 12     
  15. 13     public user(int id, String username, String password) {
  16. 14  <mappers>
  17.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  18.   </mappers> super();
  19. 15  <mappers>
  20.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  21.   </mappers> this.id = id;
  22. 16  <mappers>
  23.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  24.   </mappers> this.username = username;
  25. 17  <mappers>
  26.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  27.   </mappers> this.password = password;
  28. 18     }
  29. 19     
  30. 20     public int getId() {
  31. 21  <mappers>
  32.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  33.   </mappers> return id;
  34. 22     }
  35. 23     public void setId(int id) {
  36. 24  <mappers>
  37.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  38.   </mappers> this.id = id;
  39. 25     }
  40. 26     public String getUsername() {
  41. 27  <mappers>
  42.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  43.   </mappers> return username;
  44. 28     }
  45. 29     public void setUsername(String username) {
  46. 30  <mappers>
  47.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  48.   </mappers> this.username = username;
  49. 31     }
  50. 32     public String getPassword() {
  51. 33  <mappers>
  52.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  53.   </mappers> return password;
  54. 34     }
  55. 35     public void setPassword(String password) {
  56. 36  <mappers>
  57.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  58.   </mappers> this.password = password;
  59. 37     }
  60. 38     
  61. 39     @Override
  62. 40     public String toString() {
  63. 41  <mappers>
  64.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  65.   </mappers> return "user [id=" + id + ", username=" + username + ", password=" + password + "]";
  66. 42     }
  67. 43 }
复制代码
(2)编写一个Mapper接口UserMapper
  1. 1 package com.jms.dao;
  2. 2
  3. 3 import java.util.List;
  4. 4
  5. 5 import com.jms.pojo.User;
  6. 6
  7. 7 public interface UserMapper {
  8. 8     
  9. 9     List<User> getUsers();
  10. 10
  11. 11 }
复制代码
(3)编写Mapper配置文件UserMapper.xml
一般来说接口都会有实现类,但是这里我们用Mapper配置文件来代替了实现类。
  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. <mapper namespace="com.jms.dao.userMapper">
  6.   
  7.   <select id="getUsers" resultType="com.jms.pojo.User">
  8.     select * from mybaties.user
  9.   </select>
  10. </mapper>
复制代码
4.注册Mapper
修改mybatis的核心配置文件mybatis-config.xml,添加以下三行:
  1.   <mappers>
  2.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  3.   </mappers>
复制代码
resource对应的是实现接口的Mapper的路径。
5.junit测试
建立测试类UserMapperTest.class
  1. package com.jms.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.jms.pojo.User;import com.jms.utils.MyBatisUtil;public class UserMapperTest {  <mappers>
  2.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  3.   </mappers>@Test    public void test() {  <mappers>
  4.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  5.   </mappers>//利用工具类获取SqlSession  <mappers>
  6.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  7.   </mappers>SqlSession sqlSession = MyBatisUtil.getSqlSession();  <mappers>
  8.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  9.   </mappers>  <mappers>
  10.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  11.   </mappers>try {  <mappers>
  12.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  13.   </mappers>    //方法一  <mappers>
  14.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  15.   </mappers>    //利用SqlSession获取UserMapper接口  <mappers>
  16.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  17.   </mappers>    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);  <mappers>
  18.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  19.   </mappers>    //调用方法  <mappers>
  20.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  21.   </mappers>    List users = userMapper.getUsers();  <mappers>
  22.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  23.   </mappers>  <mappers>
  24.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  25.   </mappers>  <mappers>
  26.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  27.   </mappers>//方法二,老方法不推荐用  <mappers>
  28.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  29.   </mappers>    List users2 = sqlSession.selectList("com.jms.dao.UserMapper.getUsers");  <mappers>
  30.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  31.   </mappers>  <mappers>
  32.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  33.   </mappers>  <mappers>
  34.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  35.   </mappers>System.out.println("这是方法一的结果");  <mappers>
  36.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  37.   </mappers>    for(User user: users) {  <mappers>
  38.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  39.   </mappers>  <mappers>
  40.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  41.   </mappers>System.out.println(user);  <mappers>
  42.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  43.   </mappers>    }  <mappers>
  44.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  45.   </mappers>    System.out.println("这是方法二的结果");  <mappers>
  46.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  47.   </mappers>    for(User user: users2) {  <mappers>
  48.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  49.   </mappers>  <mappers>
  50.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  51.   </mappers>System.out.println(user);  <mappers>
  52.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  53.   </mappers>    }  <mappers>
  54.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  55.   </mappers>  <mappers>
  56.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  57.   </mappers>    } catch(Exception e) {  <mappers>
  58.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  59.   </mappers>    e.printStackTrace();  <mappers>
  60.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  61.   </mappers>} finally{  <mappers>
  62.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  63.   </mappers>    sqlSession.close();  <mappers>
  64.     <mapper resource="com/jms/dao/UserMapper.xml"/>
  65.   </mappers>}    }    }
复制代码
上面有两种方法,方法二不推荐。
SqlSession用完后应及时关闭。
测试结果如下:

 
 可以看到成功地查到了数据库中的信息。
 
(本文仅作个人学习记录用,如有纰漏敬请指正)
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

嚴華

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表