流程

  1. 编写核心配置文件
  2. 编写·sql映射配置文件
  3. 创建模型类
  4. 编写主程序代码

核心配置文件

  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. <!--数据库连接配置-->
  11. <property name="driver" value="com.mysql.jdbc.Driver"/>
  12. <property name="url" value="jdbc:mysql://10.30.59.136/wy?serverTimezone=GMT"/>
  13. <property name="username" value="root"/>
  14. <property name="password" value="123456"/>
  15. </dataSource>
  16. </environment>
  17. </environments>
  18. <!--sql映射文件路径-->
  19. <mappers>
  20. <mapper resource="UserMapper.xml"/>
  21. </mappers>
  22. </configuration>

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. <!--设置名称空间-->
  6. <mapper namespace="test">
  7. <!--查询语句并设置唯一ID号-->
  8. <select id="selectzb" resultType="com.wy.demo.User">
  9. select * from zhibo
  10. </select>
  11. </mapper>

模型类

  1. package com.wy.demo;/*
  2. coding: utf-8
  3. Created: by WAY on 2021/11/29 16:01
  4. */
  5. public class User {
  6. private int id;
  7. private String game_name;
  8. private String name;
  9. private String title;
  10. private String hot;
  11. private String pt;
  12. @Override
  13. public String toString() {
  14. return "User{" +
  15. "id=" + id +
  16. ", game_name='" + game_name + '\'' +
  17. ", name='" + name + '\'' +
  18. ", title='" + title + '\'' +
  19. ", hot='" + hot + '\'' +
  20. ", pt='" + pt + '\'' +
  21. '}';
  22. }
  23. public int getId() {
  24. return id;
  25. }
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29. public String getGame_name() {
  30. return game_name;
  31. }
  32. public void setGame_name(String game_name) {
  33. this.game_name = game_name;
  34. }
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. public String getTitle() {
  42. return title;
  43. }
  44. public void setTitle(String title) {
  45. this.title = title;
  46. }
  47. public String getHot() {
  48. return hot;
  49. }
  50. public void setHot(String hot) {
  51. this.hot = hot;
  52. }
  53. public String getPt() {
  54. return pt;
  55. }
  56. public void setPt(String pt) {
  57. this.pt = pt;
  58. }
  59. }

主程序编写

  1. package com.wy.test;
  2. /*
  3. coding: utf-8
  4. Created: by WAY on 2021/11/29 16:42
  5. */
  6. import com.wy.demo.User;
  7. import org.apache.ibatis.io.Resources;
  8. import org.apache.ibatis.session.SqlSession;
  9. import org.apache.ibatis.session.SqlSessionFactory;
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.List;
  14. public class test {
  15. public static void main(String[] args) throws IOException {
  16. //设置核心配置文件路径
  17. String resource = "mybatis-config.xml";
  18. InputStream inputStream = Resources.getResourceAsStream(resource);
  19. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  20. //获取SqlSession对象,用来执行sql
  21. SqlSession sqlsession = sqlSessionFactory.openSession();
  22. //执行sql
  23. List<User> users = sqlsession.selectList("test.selectzb");
  24. for(int i=0;i<users.size();i++){
  25. System.out.println(users.get(i));
  26. }
  27. //释放资源
  28. sqlsession.close();
  29. }
  30. }

Mapper接口代理

  1. 定义与SQL映射文件同名的Mapper接口,将接口和SQL映射文件放在同一目录下
  2. 设置SQL映射文件的名称空间为Mapper接口全限定名
  3. 在Mapper接口中定义方法,方法名为SQL映射文件中sql语句的ID,返回值类型为List<模型类>
  4. 调用SqlSession中的getMapper方法获取代理对象执行sql语句

    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. <!--设置名称空间为Mapper接口全限定名-->
    6. <mapper namespace="com.wy.Mapper.UserMapper">
    7. <!--查询语句并设置唯一ID号-->
    8. <select id="selectzb" resultType="com.wy.demo.User">
    9. select * from zhibo
    10. </select>
    11. </mapper>

    接口

    ```java package com.wy.Mapper;

import com.wy.demo.User;

import java.util.List;

public interface UserMapper { List selectzb(); }

  1. <a name="bnvXA"></a>
  2. ## 执行sql语句
  3. ```java
  4. package com.wy.test;
  5. /*
  6. coding: utf-8
  7. Created: by WAY on 2021/11/29 16:42
  8. */
  9. import com.wy.Mapper.UserMapper;
  10. import com.wy.demo.User;
  11. import org.apache.ibatis.io.Resources;
  12. import org.apache.ibatis.session.SqlSession;
  13. import org.apache.ibatis.session.SqlSessionFactory;
  14. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.util.List;
  18. public class test {
  19. public static void main(String[] args) throws IOException {
  20. String resource = "com/wy/Mapper/mybatis-config.xml";
  21. InputStream inputStream = Resources.getResourceAsStream(resource);
  22. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  23. //获取SqlSession对象,用来执行sql
  24. SqlSession sqlsession = sqlSessionFactory.openSession();
  25. //执行sql
  26. UserMapper mapper = sqlsession.getMapper(UserMapper.class);
  27. List<User> selectzb = mapper.selectzb();
  28. for (User i:selectzb){
  29. System.out.println(i);
  30. }
  31. //释放资源
  32. sqlsession.close();
  33. }
  34. }