java——mybatis——环境搭建——xml实现方式示例

淡淡的烟草味﹌ 2024-03-31 12:15 147阅读 0赞

maven中pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>org.example</groupId>
  6. <artifactId>shujuku</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>shujuku</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <maven.compiler.target>1.8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>mysql</groupId>
  25. <artifactId>mysql-connector-java</artifactId>
  26. <version>8.0.27</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.mybatis</groupId>
  30. <artifactId>mybatis</artifactId>
  31. <version>3.5.1</version>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  36. <plugins>
  37. <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
  38. <plugin>
  39. <artifactId>maven-clean-plugin</artifactId>
  40. <version>3.1.0</version>
  41. </plugin>
  42. <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
  43. <plugin>
  44. <artifactId>maven-resources-plugin</artifactId>
  45. <version>3.0.2</version>
  46. </plugin>
  47. <plugin>
  48. <artifactId>maven-compiler-plugin</artifactId>
  49. <version>3.8.0</version>
  50. </plugin>
  51. <plugin>
  52. <artifactId>maven-surefire-plugin</artifactId>
  53. <version>2.22.1</version>
  54. </plugin>
  55. <plugin>
  56. <artifactId>maven-jar-plugin</artifactId>
  57. <version>3.0.2</version>
  58. </plugin>
  59. <plugin>
  60. <artifactId>maven-install-plugin</artifactId>
  61. <version>2.5.2</version>
  62. </plugin>
  63. <plugin>
  64. <artifactId>maven-deploy-plugin</artifactId>
  65. <version>2.8.2</version>
  66. </plugin>
  67. <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
  68. <plugin>
  69. <artifactId>maven-site-plugin</artifactId>
  70. <version>3.7.1</version>
  71. </plugin>
  72. <plugin>
  73. <artifactId>maven-project-info-reports-plugin</artifactId>
  74. <version>3.0.0</version>
  75. </plugin>
  76. </plugins>
  77. </pluginManagement>
  78. </build>
  79. </project>

sun:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>sunxl</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>8</maven.compiler.source>
  11. <maven.compiler.target>8</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.11</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>mysql</groupId>
  22. <artifactId>mysql-connector-java</artifactId>
  23. <version>8.0.27</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.mybatis</groupId>
  27. <artifactId>mybatis</artifactId>
  28. <version>3.5.1</version>
  29. </dependency>
  30. </dependencies>
  31. </project>

c1d9227924c8427b641b817dc079c2f1.png

9ddafde1391e0a0ec44b361a4fd3c98f.png

创建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. <!-- 配置mybatis运行环境 -->
  6. <environments default="development">
  7. <environment id="development">
  8. <!-- 使用JDBC的事务管理 -->
  9. <transactionManager type="JDBC" />
  10. <dataSource type="POOLED">
  11. <!-- MySQL数据库驱动 -->
  12. <property name="driver" value="com.mysql.jdbc.Driver" />
  13. <!-- 连接数据库的URL -->
  14. <property name="url" value="jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8" />
  15. <property name="username" value="root" />
  16. <property name="password" value="123456" />
  17. </dataSource>
  18. </environment>
  19. </environments>
  20. <!-- 将mapper文件加入到配置文件中 -->
  21. <mappers>
  22. <mapper resource="com/sunxl/dao/UserDao.xml"/>
  23. </mappers>
  24. </configuration>

1b1db3e31e8854c5549837fa056fffa8.png

创建pojo类:

  1. package com.sunxl.pojo;
  2. public class User {
  3. Integer id;
  4. String name;
  5. Integer age;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public Integer getAge() {
  19. return age;
  20. }
  21. public void setAge(Integer age) {
  22. this.age = age;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Users{" +
  27. "id=" + id +
  28. ", name='" + name + '\'' +
  29. ", age=" + age +
  30. '}';
  31. }
  32. }

69c5ac1634373cee5be9a6039aca5636.png

创建dao接口:

  1. package com.sunxl.dao;
  2. import com.sunxl.pojo.User;
  3. import java.util.List;
  4. public interface UserDao {
  5. List<User> findAll();
  6. }

2009324ff741789656e4dff050f95a10.png

创建映射文件UserDao.xml:

  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.sunxl.dao.UserDao">
  6. <!-- 查询所有网站信息 -->
  7. <select id="findAll" resultType="com.sunxl.pojo.User">
  8. select * from USERs
  9. </select>
  10. </mapper>

47ce0aacae1c2f0d97b17148b112f121.png

创建测试:

  1. import com.sunxl.dao.UserDao;
  2. import com.sunxl.pojo.User;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.List;
  10. public class MybatisTest {
  11. public static void main(String[] args) throws IOException {
  12. InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
  13. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  14. SqlSessionFactory factory = builder.build(config);
  15. SqlSession ss = factory.openSession();
  16. UserDao userdao = ss.getMapper(UserDao.class);
  17. List<User> users = userdao.findAll();
  18. for (User user: users){
  19. System.out.println(user);
  20. }
  21. ss.commit();
  22. //4. 释放资源
  23. ss.close();
  24. config.close();
  25. }
  26. }

617f06fac73447a3e51c05183c6c7bd2.png

结果:

3f8ed021d768fad3070898c091c30a28.png

发表评论

表情:
评论列表 (有 0 条评论,147人围观)

还没有评论,来说两句吧...

相关阅读