1. Log
classification:
- SLF4J
- LOG4J [master]
- LOG4J2
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING [master]
- NO_LOGGING
For example to Log4j
What is Log4j?
- Apache Log4j is an open source project, through the use of Log4j, we can control the destination log information delivery is the console, files, GUI components
- We can also control the output format of each log;
- Each level is defined by a log of information, we can more carefully control the build process logs.
- Flexibility to be configured via a configuration file, without the need to modify the application code.
- In mybatis core configuration files, configuring our log!
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
- Introducing package log4j
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- log4j.properties- variety, and a search there
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ./log/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =./log/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
- test
Simple use of Log4j
- To use the class Log4j introduced package import org.apache.log4j.Logger;
- Log object, the parameters of the current class to class
static Logger logger = Logger.getLogger(Test2.class);
日志级别
logger.info("info:进入了testLog4j");
logger.debug("debug:进入了testLog4j");
logger.error("error:进入了testLog4j");
2. Paging
Limit the use of paging
grammar:.
select * from user limit #{startIndex},#{pageSize} </select>
- 1. Interface
List<User> selectUserByLimit(Map<String,Integer> map);
- 2.Mapper.xml
<select id="selectUserByLimit" parameterType="map" resultMap="UserMap">
select * from user limit #{startIndex},#{pageSize}
</select>
- 3. Test
import com.yang.mapper.RMapper;
import com.yang.pojo.User;
import com.yang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.List;
public class Test2 {
static Logger logger = Logger.getLogger(Test2.class);
public static void main(String[] args) {
SqlSession session = MyBatisUtils.getSession();
RMapper mapper = session.getMapper(RMapper.class);
// User user = mapper.selectUserById(1);
// System.out.println(user);
// System.out.println("21324534231");
// logger.error("1232142324312431");
// logger.info("13124134231");
// logger.debug("121342342312");
// User user = mapper.selectUserById(1);
// System.out.println(user);
int currentPage = 2;
int pageSize = 2;
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("startIndex",(currentPage-1)*pageSize);
hashMap.put("pageSize",pageSize);
List<User> users = mapper.selectUserByLimit(hashMap);
for (User user : users) {
logger.debug(user);
System.out.println(user);
}
}
}
Use RowBounds page
No longer implement paging using SQL
- 1. Interface
List<User> selectUserByLimit();
- 2.mapper.xml
<select id="selectUserByLimit" parameterType="map" resultMap="UserMap">
select * from user ;
</select>
<resultMap id="UserMap" type="User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--column列名,property属性名-->
<result column="pwd" property="password"/>
</resultMap>
<select id="selectUserById" resultMap="UserMap">
select id,name,pwd from user where id = #{id}
</select>
- 3. Test
import com.yang.mapper.RMapper;
import com.yang.pojo.User;
import com.yang.utils.MyBatisUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.List;
public class Test2 {
static Logger logger = Logger.getLogger(Test2.class);
public static void main(String[] args) {
SqlSession session = MyBatisUtils.getSession();
int currentPage = 2;
int pageSize = 2;
RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize,pageSize);
List<User> users = session.selectList("com.yang.mapper.RMapper.selectUserByLimit", null, rowBounds);
for (User user : users) {
logger.debug(user);
// System.out.println(user);
}
}
}
3. Use annotations development (from mapper.xml Development)
- 1. Notes implemented on the interface
package com.yang.mapper;
import com.yang.pojo.User;
import com.yang.utils.MybatisUtils;
import org.junit.Test;
import org.apache.ibatis.session.SqlSession;
public class UserMapperTest {
@Test
public void testGetUserList(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
for (User user : mapper.getUserList()) {
System.out.println(user);
}
}
@Test
public void testGetUserID(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.getUsrByID(3));
}
@Test
public void testGetUserByName(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.getUserByName("gukai"));
}
@Test
public void testAddUser(){
SqlSession session = MybatisUtils.getSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.addUser(new User(4, "蔡梓鑫", "123456")));
}
@Test
public void testupdateUser(){
SqlSession session = MybatisUtils.getSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.updateUser(new User(4, "gukai", "123456")));
}
@Test
public void testdeleteUser(){
SqlSession session = MybatisUtils.getSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.deleteUser(2));
}
}
- 2. Core configuration file binding interfaces!
<mappers>
<mapper class="com.yang.mapper.UserMapper"/>
<!--<package name="com.yang.mapper"/>-->
<!--<mapper resource="com/yang/mapper/UserMapper.xml"/>-->
</mappers>
- 3. Test
package com.yang.mapper;
import com.yang.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> getUserList();
User getUser(int id);
@Select("select * from user where name=#{name}")
User getUserByName(String name);
@Select("select * from user where id=#{id}")
User getUsrByID(int id);
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
@Update({"update user set name=#{name},pwd=#{pwd} where id=#{id}"})
int updateUser(User user);
@Delete("delete from user where id=#{id}")
int deleteUser(int id);
}
- 3. Add tools MybatisUtils, configure the transaction commits
package com.yang.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//需要提交事务则增加参数,不需要提交事务则不增加参数
public static SqlSession getSession(){
return getSession(false);
}
//自动提交事务
public static SqlSession getSession(boolean flag){
return sqlSessionFactory.openSession(flag);
}
}