<?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"> <!-- 配置事务管理器 type="jdbc" 事务 要么一起成功,要么一起失败 --> <transactionManager type="jdbc"/> <!-- 配置数据源信息 type="jdbc / pooled / unpooled" jdbc 连接方式 pooled 连接池技术--> <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 class="com.mypack.dao.UserDao"/> </mappers> </configuration>
<?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.mypack.mapper.UserMapper" namespace表示命名空间 一般写 成包名.文件名--> <mapper namespace="com.mypack.dao.StudentDao"> <!-- 配置属性和字段映射关系 一对一关系--> <resultMap type="Student" id="studentResultMap"> <id property="sid" column="s_id"/> <result property="sname" column="s_name"/> <association property="banji" javaType="BanJI"> <id property="cid" column="c_id"/> <result property="cname" column="c_name"/> </association> </resultMap> <select id="queryOneStuById" parameterType="int" resultMap="studentResultMap"> select * from student s,class c where s.class_id =c.c_id and s.s_id=#{sid} </select> </mapper>
import com.mypack.entity.Student; public interface StudentDao{ public Student queryOneStuById(int id); }
public class MyBatisUtil { public static SqlSessionFactory getSqlSessionFactory(){ // mybatis的配置文件 String resource = "mybatis.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream( resource); // 构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder() .build(is); return sessionFactory; } public static SqlSession getSqlSession(){ return getSqlSessionFactory().openSession(true); } }
log4j.rootLogger=DEBUG,CONSOLE,file #log4j.rootLogger=ERROR,ROLLING_FILE log4j.logger.cn.kgc.dao=debug log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug log4j.logger.java.sql.ResultSet=debug log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug ###################################################################################### # Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e ###################################################################################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=error log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n ###################################################################################### # DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11 ###################################################################################### log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.DatePattern=yyyy-MM-dd log4j.appender.file.File=log.log log4j.appender.file.Append=true log4j.appender.file.Threshold=error log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L)%m%n log4j.logger.com.opensymphony.xwork2=error
原文:https://www.cnblogs.com/tomcat9/p/11002644.html