继续(二)说
1.工程结构
2.新建db.properties文件(我的数据库没有设置密码)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis name=root password=
3.log4j.jar加入工程并添加log4j.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="debug" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
4.conf.xml添加了起别名,添加了db.propreties的配置
<?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="db.properties"/> <!-- 配置实体类的别名 --> <typeAliases> <!-- <typeAlias type="com.atguigu.day03_mybaits.test2.User" alias="User"/> --> <package name="com.kpzc.bean"/> </typeAliases> <!-- development : 开发模式 work : 工作模式 --> <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="${name}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <!-- 写mapper所在的包的路径 --> <mapper resource="com/kpzc/test1/userMapper.xml"/> <mapper resource="com/kpzc/test2/userMapper.xml"/> <!--<mapper class="com.atguigu.day03_mybaits.test3.UserMapper"/> <mapper resource="com/atguigu/day03_mybaits/test4/orderMapper.xml"/> <mapper resource="com/atguigu/day03_mybaits/test5/ClassMapper.xml"/> <mapper resource="com/atguigu/day03_mybaits/test6/ClassMapper.xml"/> <mapper resource="com/atguigu/day03_mybaits/test7/userMapper.xml"/> <mapper resource="com/atguigu/day03_mybaits/test8/userMapper.xml"/> <mapper resource="com/atguigu/day03_mybaits/test9/userMapper.xml"/> --> </mappers> </configuration>
5.运行查询就有log输出
时间: 2024-10-01 05:07:01