新建一个数据库配置文件db.PRoperties
driver=com.MySQL.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername=rootpassWord=123456
在主配置文件conf.xml中导入db.properties<?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"/> <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> <mappers> <mapper resource="com/zhuyun/mybatis/userMapper.xml" /> </mappers></configuration>2,配置实体类或者包的别名
1)实体类的别名
在主配置文件conf.xml中配置实体类的别名<!-- 配置实体类的别名 --> <typeAliases> <typeAlias type="com.zhuyun.mybatis.User" alias="_User"/> </typeAliases>在映射文件userMapper.xml中,即可用别名_User来代替com.zhuyun.mybatis.User<select id="getUser" parameterType="int" resultType="_User"> select * from users where id = #{id} </select>2)包的别名
在主配置文件conf.xml中配置包的别名<!-- 配置包的别名--> <typeAliases> <!-- <typeAlias type="com.zhuyun.mybatis.User" alias="_User"/> --> <package name="com.zhuyun.mybatis"/> </typeAliases>在映射文件userMapper.xml中,即可用别名User来代替com.zhuyun.mybatis.User<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.zhuyun.mybatis.userMapper"> <insert id="addUser" parameterType="User"> insert into users(name,age) values(#{name}, #{age}) </insert> <delete id="deleteUser" parameterType="int"> delete from users where id=#{id} </delete> <update id="updateUser" parameterType="User"> update users set name=#{name},age=#{age} where id=#{id} </update> <select id="getUser" parameterType="int" resultType="User"> select * from users where id = #{id} </select> <select id="getAllUsers" resultType="User"> select * from users </select></mapper>3,打印日志信息
1)添加jar包
log4j-1.2.16.jar2)添加log4j.xml文件到src下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[CateringLog] %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c - %m%n" /> </layout> </appender> <appender class="org.apache.log4j.RollingFileAppender" name="file"> <param name="File" value="d:/companyProject/logs/catering.log" /> <param name="Append" value="true" /> <param name="MaxFileSize" value="1024KB" /> <param name="MaxBackupIndex" value="5" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[CateringLog] %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c - %m%n" /> </layout> </appender> <root> <priority value="debug" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> <!--通过<logger></logger>的定义可以将各个包中的类日志输出到不同的日志文件中--> <logger name="org.springframework"> <level value="ERROR" /> </logger> <logger name="org.mybatis.spring"> <level value="ERROR" /> </logger> <logger name="net.sf.ehcache"> <level value="ERROR" /> </logger> <logger name="com.mchange.v2"> <level value="ERROR" /> </logger> <logger name="java.sql"> <level value="debug" /> </logger> </log4j:configuration>3)正常运行即可出现日志信息
新闻热点
疑难解答