java框架记录学习(Mybatis第三篇)
1.日志工厂
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
value可以取的值有:SLF4J、LOG4J、LOG4J2、JDK_LOGGING、COMMONS_LOGGING、STDOUT_LOGGING、NO_LOGGING
1.1 LOG4J
1.导入LOG4J依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.建立log4j.properties
# priority :debug<info<warn<error
#you cannot specify every priority with different file for log4j
log4j.rootLogger=debug,stdout,info,debug,warn,error
#console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
#info log
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.info.File=./src/com/hp/log/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#debug log
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.debug.File=./src/com/hp/log/debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#warn log
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.warn.File=./src/com/hp/log/warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#error
log4j.logger.error=error
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.error.File = ./src/com/hp/log/error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
3.配置实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
2.limit实现分页
sql语法:
select * from user limit startIndex,pagSize
分页查询实现:
接口类:
List<User> getUserListLimit(Map<String,Integer> map);
接口类对应的xml文件:
<select id="getUserListLimit" parameterType="map" resultMap="userMap">
select * from navy.user limit #{startIndex},#{pageSize}
</select>
测试类:
@Test
public void test1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map= new HashMap<String, Integer>();
map.put("startIndex",1);
map.put("pageSize",2);
List<User> userList = userMapper.getUserListLimit(map);
for (User user : userList) {
System.out.println(user);
}
}
3.多对一查询处理
实体类:
package com.navy.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
package com.navy.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
1.方式一(子查询):
接口对应配置文件
<select id="SelectAll" resultMap="StudentTeacher">
select * from navy.student
</select>
<resultMap id="StudentTeacher" type="com.navy.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<association property="teacher" column="tid" javaType="com.navy.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.navy.pojo.Teacher">
select * from navy.teacher where id = #{tid}
</select>
接口类:
List<Student> SelectAll();
测试类:
@Test
public void test01(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper studentmapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList= studentmapper.SelectAll();
for (Student student : studentList) {
System.out.println(student);
}
}
2.方式二(嵌套查询):
接口对应配置文件
<select id="SelectAll2" resultMap="StudentTeacher2">
select s.id as sid ,s.name as sname ,t.name tname,t.id tid
from navy.student as s,navy.teacher t
where s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="com.navy.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="com.navy.pojo.Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
</association>
</resultMap>
接口类:
List<Student> SelectAll2();
测试类:
@Test
public void test01(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper studentmapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList= studentmapper.SelectAll2();
for (Student student : studentList) {
System.out.println(student);
}
}
4.一对多查询
java实体类:
package com.mybatis.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
}
package com.mybatis.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
1.方式一:
接口类:
Teacher getTeacher(@Param("uid") int id);
xml文件:
<select id="getTeacher" resultMap="TeacherStudent">
select t.id as tid, t.name as tname,s.name as sname,s.id as sid
from navy.teacher as t,navy.student as s
where t.id = s.tid and t.id = #{uid}
</select>
<resultMap id="TeacherStudent" type="com.mybatis.pojo.Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="com.mybatis.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
2.方式二:
接口方法:
Teacher getTeacher02(@Param("uid") int id);
<select id="getTeacher02" resultMap="TeacherStudent02">
select * from navy.teacher where id = #{uid};
</select>
<resultMap id="TeacherStudent02" type="com.mybatis.pojo.Teacher">
<collection property="students" javaType="ArrayList" ofType="com.mybatis.pojo.Student" select="getstudent" column="id"/>
</resultMap>
<select id="getstudent" resultType="com.mybatis.pojo.Student">
select * from navy.student where tid = #{id};
</select>
注:JavaType 用来指定实体类中属性的类型
ofType用来指定映射List或者集合中的pojp类型,泛型中的约束型。