Mybatis配置文件详解

迷南。 2022-02-15 08:45 407阅读 0赞

一、目录结构
在这里插入图片描述

二、代码详解

1.maven核心配置文件

  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>com.mollen.mybatis</groupId>
  7. <artifactId>mybatis</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.mybatis</groupId>
  12. <artifactId>mybatis</artifactId>
  13. <version>3.4.5</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>mysql</groupId>
  17. <artifactId>mysql-connector-java</artifactId>
  18. <version>5.1.38</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>log4j</groupId>
  22. <artifactId>log4j</artifactId>
  23. <version>1.2.17</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <artifactId>junit</artifactId>
  28. <version>4.11</version>
  29. </dependency>
  30. </dependencies>
  31. <!-- 可以解决idea不能编译src目录下的xml文件 -->
  32. <build>
  33. <!--加载src目录下的静态文件-->
  34. <resources>
  35. <resource>
  36. <directory>src/main/java</directory>
  37. <includes>
  38. <include>**/*.xml</include>
  39. </includes>
  40. </resource>
  41. </resources>
  42. <plugins>
  43. <!--jdk编译版本设置-->
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-compiler-plugin</artifactId>
  47. <configuration>
  48. <source>1.8</source>
  49. <target>1.8</target>
  50. <encoding>utf-8</encoding>
  51. </configuration>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>

2.mybatis核心配置文件:SqlMapConfig.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. <!--mybatis环境配置-->
  5. <configuration>
  6. <!--加载数据源文件-->
  7. <properties resource="db.properties"></properties>
  8. <!--配置别名-->
  9. <typeAliases>
  10. <package name="com.mollen.dao"/>
  11. <package name="com.mollen.bean"/>
  12. </typeAliases>
  13. <!--配置数据源-->
  14. <environments default="mysql">
  15. <environment id="mysql">
  16. <transactionManager type="JDBC"></transactionManager>
  17. <dataSource type="POOLED">
  18. <property name="driver" value="${jdbc.driver}"></property>
  19. <property name="url" value="${jdbc.url}"></property>
  20. <property name="username" value="${jdbc.username}"></property>
  21. <property name="password" value="${jdbc.password}"></property>
  22. </dataSource>
  23. </environment>
  24. </environments>
  25. <!--配置映射/-->
  26. <mappers>
  27. <!--单个映射配置-->
  28. <!--<mapper resource="com/mollen/dao/UserDao.xml"/>-->
  29. <!--批量映射配置-->
  30. <package name="com.mollen.dao"/>
  31. </mappers>
  32. </configuration>

3.配置数据源文件

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatis_test
  3. jdbc.username=root
  4. jdbc.password=root

4.映射文件配置:UserDao.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. <!--配置需要映射的接口的完全限定名称-->
  5. <mapper namespace="com.mollen.dao.UserDao">
  6. <!--配置 查询结果的列名和实体类的属性名的对应关系/暂不考虑多表映射-->
  7. <resultMap id="userMap" type="user">
  8. <!--主键字段的对应-->
  9. <id property="id" column="id"></id>
  10. <!-- 非主键字段的对应-->
  11. <result property="username" column="username"></result>
  12. <result property="birthday" column="birthday"></result>
  13. <result property="sex" column="sex"></result>
  14. <result property="address" column="address"></result>
  15. </resultMap>
  16. <!--查询所有-->
  17. <select id="findAll" resultType="user">
  18. SELECT * FROM user;
  19. </select>
  20. <!--根据id查询用户-->
  21. <select id="findUserById" resultMap="userMap">
  22. SELECT * FROM user WHERE id in
  23. <foreach collection="array" open="(" close=")" separator="," item="uid">
  24. #{uid}
  25. </foreach>
  26. </select>
  27. <!--根据用户模糊查询-->
  28. <select id="findByName" resultMap="userMap">
  29. select * from user where username like #{name}
  30. </select>
  31. <!--根据条件模糊查询-->
  32. <select id="findByCondition" resultMap="userMap">
  33. SELECT * FROM user WHERE 1=1
  34. <if test="username != null">
  35. AND username like #{username}
  36. </if>
  37. <if test="sex != null">
  38. AND sex like #{sex}
  39. </if>
  40. </select>
  41. <!--添加用户-->
  42. <insert id="saveOne">
  43. INSERT INTO
  44. user
  45. VALUES
  46. (
  47. null,
  48. #{username},
  49. #{birthday},
  50. #{sex},
  51. #{address}
  52. )
  53. </insert>
  54. <!--删除用户/根据传入参数类型自动匹配sql参数值-->
  55. <delete id="deleteOne" >
  56. DELETE FROM
  57. user
  58. WHERE
  59. id = #{id}
  60. </delete>
  61. <!--修改用户-->
  62. <update id="updateUser">
  63. UPDATE
  64. user
  65. SET
  66. username = #{username},
  67. birthday = #{birthday},
  68. sex = #{sex},
  69. address = #{address}
  70. WHERE
  71. id = #{id}
  72. </update>
  73. </mapper>

5.Dao接口类:UserDao.java

  1. package com.mollen.dao;
  2. import com.mollen.bean.User;
  3. import java.util.List;
  4. /**
  5. * @ClassName: UserDao
  6. * @Auther: Mollen
  7. * @CreateTime: 2018-10-27 20:19:07
  8. * @Description:
  9. */
  10. public interface UserDao {
  11. /**
  12. * 1.查询所有
  13. * @return
  14. */
  15. public List<User> findAll();
  16. /**
  17. * 2.根据id查询用户(动态执行参数)
  18. * @return
  19. */
  20. public List<User> findUserById(Integer...ids);
  21. /**
  22. * 3.根据用户模糊查询
  23. */
  24. public List<User> findByName(String username);
  25. /**
  26. * 4.根据条件模糊查询
  27. * @param user
  28. * @return
  29. */
  30. public List<User> findByCondition(User user);
  31. /**
  32. * 5.添加一条数据
  33. * @param user
  34. */
  35. public void saveOne(User user);
  36. /**
  37. * 6.删除一条数据
  38. * @param user
  39. */
  40. public void deleteOne(User user);
  41. /**
  42. * 7.修改用户
  43. */
  44. public void updateUser(User user);
  45. }

6.实体类:User.java

  1. package com.mollen.bean;
  2. import java.util.Date;
  3. /**
  4. * @ClassName: User
  5. * @Auther: Mollen
  6. * @CreateTime: 2018-10-29 10:11:48
  7. * @Description:
  8. */
  9. public class User {
  10. private int id;
  11. private String username;
  12. private Date birthday;
  13. private String sex;
  14. private String address;
  15. //getter/setter/toString/...
  16. }

原文:https://blog.csdn.net/mollen/article/details/84566676

发表评论

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

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

相关阅读

    相关 Mybatis配置文件详解

    配置文件和映射文件还有挺多的属性我还没有讲的,现在就把它们一一补全 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为Mapped