一:整合日志含有log4j和logback
logback是log4j的升级版他性能提升较大,有些甚至达到10倍以上,占的内存更小,slf4j能很好的整合它,还有很多数不胜数的优势
1.下载jar包,依赖:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
2.log4j.properties配置文件
### direct log messages to stdout ###
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=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/log.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.logger.cn.dawn=debug, stdout,file
###log4j.rootLogger=debug, stdout,file###
3.logback
1.jar包(所需要的依赖)
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
2.logback.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 尽量别用绝对路径,如果带参数不同容器路径解释可能不同,以下配置参数在pom.xml里 -->
<!-- <property name="log.root.level" value="DEBUG" /> 日志级别
<property name="log.other.level" value="DEBUG" /> 其他日志级别 -->
<!-- <property name="log.base" value="logs" /> 日志路径,这里是相对路径,web项目eclipse下会输出到eclipse的安装目录下,如果部署到linux上的tomcat下,会输出到tomcat/bin目录 下
<property name="log.moduleName" value="OALog" /> 模块名称, 影响日志配置名,日志文件名
<property name="log.max.size" value="100MB" /> 日志文件大小
-->
<!--控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger{56}.%method:%L -%msg%n</Pattern>
</encoder>
</appender> <!-- 日志文件输出 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>logs/OALog.log</File><!-- 设置日志不超过${log.max.size}时的保存路径,注意如果 是web项目会保存到Tomcat的bin目录 下 -->
<!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/archive/OALog_all_%d{yyyy-MM-dd}.%i.log.zip
</FileNamePattern>
<!-- 当天的日志大小 超过${log.max.size}时,压缩日志并保存 -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<!-- 日志输出的文件的格式 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger{56}.%method:%L -%msg%n</pattern>
</layout>
</appender> <!-- 为某个包下的所有类的指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName -->
<!--myibatis log configure-->
<!-- <logger name="com.apache.ibatis" level="trace"/>
<logger name="java.sql.Connection" level="DEBUG"/>
<logger name="java.sql.Statement" level="DEBUG"/>
<logger name="java.sql.PreparedStatement" level="DEBUG"/>-->
<logger name="cn.dawn" additivity="true"> //要改成自己项目的dao层全类名
<level value="debug" />
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</logger>
<!-- root将级别为“DEBUG”及大于“DEBUG”的日志信息交给已经配置好的名为“Console”的appender处理,“Console”appender将信息打印到Console -->
<!--<root level="info">
<appender-ref ref="stdout" /> <!– 标识这个appender将会添加到这个logger –>
<appender-ref ref="file" />
</root>-->
</configuration>
二:SQL的操作
1: sql片段解决 * sql的查询语句列名太多很麻烦解决的方案:
<sql id="mysql">
bookId,bookName,bookAuthor,bookPrice
</sql>
<select id="findOneBookByPK" resultType="Book" >
SELECT <include refid="mysql"/> FROM book WHERE bookid=#{bookID} //include将sql片段中的列名引用过去
</select>
2:模糊查询
dao层:public List<Book> likeSelect(String bookName);
小配置中:
<sql id="mysql">
bookId,bookName,bookAuthor,bookPrice
</sql>
<!--模糊查询-->
<!--不可以防止sql注入
<select id="likeSelect" parameterType="string" resultType="Book">
SELECT * FROM book WHERE bookName LIKE '%${value}%'
</select>-->
<!--可以防止sql注入
<select id="likeSelect" parameterType="string" resultType="Book">
SELECT * FROM book WHERE bookName LIKE '%' #{value} '%'
</select>-->
<!--可以防止sql注入-->
<select id="likeSelect" parameterType="string" resultType="Book">
SELECT <include refid="mysql"/> FROM book WHERE bookName LIKE concat('%',#{bookName},'%')
</select>
测试类中:
@Test
public void t6likeSelect(){ String path = "mybatis-config.xml"; try {
InputStream e = Resources.getResourceAsStream(path);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(e);
SqlSession session = factory.openSession(); IBookDAO mapper = session.getMapper(IBookDAO.class); session.getMapper(dao层接口.class)反射的方式获取到了dao层这个接口的实现类,所以就可以使用强类型的方式,直接通过点的方式点出来供你选择。例如mapper.findAll()
List<Book> list = mapper.likeSelect("的");
for (Book item:list) {
System.out.println(item.getBookName());
} session.close();
} catch (IOException var9) {
var9.printStackTrace();
}
}
三:resultMap
在接口中的方法声明如下:public List<Book> findAllBook();
在小配置中如下:
<!--resultmap-->
<resultMap id="bookMapper" type="Book">
<id column="bookID" property="bookID"></id>
<!--默认的自动映射行为是PARTIAL部分映射,就不用写bookname也能装配到-->
</resultMap>
<!--resultmap-->
<select id="findAllBook" resultMap="bookMapper">
select * from book
</select>
他默认的自动映射行为是PARTIAL,部分的,也就是开启的,他也可以手动设置,关闭自动映射行为
设置方法是在大配置文件中
<!--<settings>
自动映射行为
<setting name="autoMappingBehavior" value="NONE"/>
</settings>-->
它的意思是关闭NONE,这个不用关,所以我就是简单的操作后就注释掉了
测试类中
@Test
public void t1SelectAll(){
String path = "mybatis-config.xml"; try {
InputStream e = Resources.getResourceAsStream(path);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(e);
SqlSession session = factory.openSession();
IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> list = mapper.findAllBook();
for (Book item:list) {
System.out.println(item.getBookName());
} session.close();
} catch (IOException var9) {
var9.printStackTrace();
}
} 四:多条件查询
实体类:
public class Book {
private Integer bookID;
private String bookName;
private String bookAuthor;
private Integer bookPrice;
}
接口中的方法
俩种形式,一种采用map,一种采用直接参数Index(索引的方式)来实现的多条件查询
//根据多条件查询map版
public List<Book> findtrueBookMap(Map<String,Object> map);
//根据多条件查询index版
public List<Book> findtrueBookIndex(String bookName,Integer bookPrice);
小配置中
<!--多条件查询map版-->
<select id="findtrueBookMap" resultType="Book">
select * from book WHERE bookName LIKE '%' #{bookName} '%' AND bookPrice>#{bookPrice}
</select>
<!--多条件查询Index版-->
<select id="findtrueBookIndex" resultType="Book">
select * from book WHERE bookName LIKE '%' #{0} '%' AND bookPrice>#{1}
</select>
测试类中
///多条件查询Index版
@Test
public void t4selectmoreIndex(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> books = mapper.findtrueBookIndex("心",40);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///多条件查询map版
@Test
public void t3selectmoreMap(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("bookName","心");
map.put("bookPrice",40);
List<Book> books = mapper.findtrueBookMap(map);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); }
五:智能标签分为 where ,if ,choose ,foreach的array方式 ,foreach的list方式 ,foreach的list自定义类型方式
实体类
public class Book {
private Integer bookID;
private String bookName;
private String bookAuthor;
private Integer bookPrice; public Book() {
} public Integer getBookID() {
return this.bookID;
} public void setBookID(Integer bookID) {
this.bookID = bookID;
} public String getBookName() {
return this.bookName;
} public void setBookName(String bookName) {
this.bookName = bookName;
} public String getBookAuthor() {
return this.bookAuthor;
} public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
} public Integer getBookPrice() {
return this.bookPrice;
} public void setBookPrice(Integer bookPrice) {
this.bookPrice = bookPrice;
}
}
接口中的方法
//智能标签where if
public List<Book> findtrueBookByIf(String bookName,Integer bookPrice);
//智能标签where choose
public List<Book> findtrueBookByChoose(Integer bookPrice);
//智能标签where foreach array
public List<Book> findtrueBookByForeachArray(int [] array);
//智能标签where foreach list
public List<Book> findtrueBookByForeachList(List<Integer> list);
//智能标签where foreach list
public List<Book> findtrueBookByForeachListBook(List<Book> list);
小配置中
<!--智能标签,where if-->
<select id="findtrueBookByIf" resultType="Book">
select * from book
<where>
<if test="#{0}!=null">
AND bookName LIKE '%' #{0} '%'
</if>
<if test="#{0}!=null">
AND bookPrice>#{1}
</if>
</where>
</select>
<!--智能标签,where choose-->
<select id="findtrueBookByChoose" resultType="Book">
select * from book
<where>
<choose>
<when test="#{0}!=null">
AND bookPrice>#{0}
</when>
<otherwise>1=1</otherwise>
</choose>
</where>
</select>
<!--智能标签,where foreach array-->
<select id="findtrueBookByForeachArray" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="array" open="(" close=")" separator="," item="myid">
#{myid}
</foreach>
</where>
</select>
<!--智能标签,where foreach list-->
<select id="findtrueBookByForeachList" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="list" open="(" close=")" separator="," item="myid">
#{myid}
</foreach>
</where>
</select>
<!--智能标签,where foreach list book-->
<select id="findtrueBookByForeachListBook" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="list" open="(" close=")" separator="," item="book">
#{book.bookID}
</foreach>
</where>
</select>
测试类中
///智能标签where + foreach list Book自定义list 进行多条件查询
@Test
public void t9selectZhiNengByForeachListBook(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> list=new ArrayList<Book>();
Book b1=new Book();
b1.setBookID(1);
Book b2=new Book();
b2.setBookID(2);
list.add(b1);
list.add(b2);
List<Book> books = mapper.findtrueBookByForeachListBook(list);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + foreach list 进行多条件查询
@Test
public void t8selectZhiNengByForeachList(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(3);
List<Book> books = mapper.findtrueBookByForeachList(list);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + foreach array 进行多条件查询
@Test
public void t7selectZhiNengByForeachArray(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
int[] array={1,3};
List<Book> books = mapper.findtrueBookByForeachArray(array);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + choose进行多条件查询
@Test
public void t6selectZhiNengByChoose(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> books = mapper.findtrueBookByChoose(500);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + if 进行多条件查询
@Test
public void t5selectZhiNengByIf(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> books = mapper.findtrueBookByIf("心",40);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); }
六: