其他分享
首页 > 其他分享> > 在IDEA中配置Mybatis项目

在IDEA中配置Mybatis项目

作者:互联网

1. 创建一个简单的maven工程

在这里插入图片描述)
在这里插入图片描述)

2. 在生成的pom.xml文件中导入mybatis需要的相关依赖

在这里插入图片描述)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mybatis</artifactId>
        <groupId>com.zjx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mybatis-01</artifactId>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
		<!--连接数据库所需要的jar包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
		<!--测试单元-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- 
		日志文件
		https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- 
		简化实体类的初始化
		https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <!--解决静态资源导出问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

依赖导入完毕后
在这里插入图片描述)

3.创建mybatis的核心配置文件

建议参考mybatis参考文档:mybatis中文参考文档

先看一下一个Mybatis工程长啥样

java下

在这里插入图片描述)

resource下

在这里插入图片描述)

官方文档的核心配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!--这里的内容基本不需要改,接下来是增加一些配置-->
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
      	<!--这里是用来连接数据库的-->
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <!--每一个Mapper接口都要对应一个Mapper.xml文件映射-->
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

给xml文件增加一些配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    
    <!--加载数据库连接的配置文件-->
    <properties resource="jdbc.properties"/>

    <!--日志设置-->
    <settings>
        <!--全局性地开启或关闭所有映射器配置文件中已配置的任何缓存-->
        <setting name="cacheEnabled" value="true"/>
        <!--标准的日志工厂的实现-->
        <setting name="logImpl" value="LOG4J"/>
        <!--是否开启驼峰命名自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--配置别名-->
    <typeAliases>
<!--        <typeAlias type="com.zjx.pojo.User" alias="User"/>-->
        <package name="com.zjx.pojo"/>
    </typeAliases>

    <!-- 配置开发环境,可以配置多个,在具体用时再做切换 -->
    <environments default="test">
        <environment id="test">
            <transactionManager type="JDBC"></transactionManager>    <!-- 事务管理类型:JDBC、MANAGED -->
            <dataSource type="POOLED">    <!-- 数据源类型:POOLED、UNPOOLED、JNDI -->
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>

    <!-- 加载映射文件 mapper -->
    <mappers>
        <mapper resource="com/zjx/dao/UserMapper.xml"/>
        <mapper resource="com/zjx/dao/TeacherMapper.xml"/>
        <mapper resource="com/zjx/dao/BlogMapper.xml"/>
    </mappers>
</configuration>

jdbc配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xk?characterEncoding=utf-8
username=root
password=123456

log4j配置文件(不需要改,直接复制即可)


log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE


log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug

log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n


log4j.appender.fileAppender = org.apache.log4j.FileAppender
log4j.appender.fileAppender.File = ./logs/mybatis.log
log4j.appender.fileAppender.Append = true
log4j.appender.fileAppender.Encoding = UTF-8
log4j.appender.fileAppender.Threshold = DEBUG
log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRollingFile.File = ./logs/mybatis.log
log4j.appender.dailyRollingFile.Append = true
log4j.appender.dailyRollingFile.Encoding = UTF-8
log4j.appender.dailyRollingFile.Threshold = DEBUG
log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


log4j.appender.systemFile.DatePattern = '.'yyyy-MM-dd 

log4j.appender.systemFile.layout.ConversionPattern=[%5p] %d{dd-MM-yy HH:mm:ss} : %F %L : %m %n
log4j.appender.systemFile.File=./logs/mybatis.log


log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.Encoding=UTF-8
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

一个简单的接口对应的mapper.xml,文件所表示的信息建议参考文章开头的mybatis中文官方文档

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.zjx.dao.UserMapper">

    <!--结果集映射-->
    <resultMap id="UserMap" type="com.zjx.pojo.User">
        <result column="userID" property="userID"/>
        <result column="userName" property="userName"/>
        <result column="balance" property="balance"/>
    </resultMap>


    <!--查询所有的信息-->
    <select id="getAllUser" resultMap="UserMap">
        select * from xk.user1;
    </select>
    <update id="updateByID" parameterType="com.zjx.pojo.User">
        update user1 set userName = #{userName}, balance = #{balance} where userID = #{userID};
    </update>
    <delete id="deleteByID" parameterType="String">
        delete from user1 where userID = #{id};
    </delete>
    <insert id="addUser" parameterType="com.zjx.pojo.User">
        insert into user1(userID, userName, balance) values(#{userID}, #{userName}, #{balance});
    </insert>
</mapper>

基本的配置情况就是这些

标签:配置,IDEA,log4j,mybatis,ROLLING,FILE,Mybatis,org,appender
来源: https://blog.csdn.net/star_zhang_jx/article/details/121081185