1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
### 设置Logger输出级别和输出目的地 ### log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender #log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout ### 把日志信息输出到文件:jbit.log ### log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=jbit.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n ###显示SQL语句部分 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 |
1
2
3
4
5
6
7
8
9
10 |
< dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >1.2.17</ version > </ dependency > < dependency > < groupId >commons-logging</ groupId > < artifactId >commons-logging</ artifactId > < version >1.1.3</ version > </ dependency > |
1 |
public Posts getByParams(Posts posts); |
1
2
3
4
5
6
7
8
9
10 |
< select id = "getByParams" parameterType = "Posts" resultType = "Posts" > select * from posts where 1=1 < if test = "title!=null" > and title=#{title} </ if > < if test = "context!=null" > and context=#{title} </ if > limit 0,1 </ select > |
1 |
public Posts getByChoose(Posts post); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
< select id = "getByChoose" parameterType = "Posts" resultType = "Posts" > select * from posts where 1=1 < choose > < when test = "title!=null" > and title=#{title} </ when > < when test = "context!=null" > and context=#{title} </ when > < otherwise > and 1=1 </ otherwise > </ choose > limit 0,1 </ select > |
1
2
3
4
5
6
7
8
9
10 |
<select id= "getByParams" parameterType= "Posts" resultType= "Posts" > select * from posts where 1 = 1 < if test= "title!=null" > and title=#{title} </ if > < if test= "context!=null" > and context=#{title} </ if > limit 0 , 1 </select> |
1
2
3
4
5
6
7
8
9
10
11
12 |
<select id= "getByParams" parameterType= "Posts" resultType= "Posts" > select * from posts <where> < if test= "title!=null" > and title=#{title} </ if > < if test= "context!=null" > and context=#{title} </ if > </where> limit 0 , 1 </select> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
<!-- 等效于where --> <select id= "trimTest" parameterType= "Posts" resultType= "Posts" > select * from posts <trim prefix= "where" prefixOverrides= "and|or" suffix= "and 1=1" > < if test= "title!=null" > title=#{title} </ if > < if test= "context!=null" > and context=#{context} </ if > < if test= "id!=0" > or id=#{id} </ if > </trim> limit 0 , 1 </select> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
<update id= "update" parameterType= "Posts" > update posts <set> < if test= "context!=null" > context=#{context}, </ if > < if test= "title!=null" > title=#{title}, </ if > < if test= "badCount!=0" > badcount=#{badCount}, </ if > </set> where id=#{id} </update> |
1
2
3
4
5
6 |
<update id= "batchUpdate" parameterType= "java.util.List" > update posts set badcount= 3 ,goodcount= 5 where id in <foreach collection= "list" item= "item" open= "(" close= ")" index= "index" separator= "," > #{item.id} </foreach> </update> |
1
2
3
4
5
6 |
<update id= "updateArray" > update posts set badcount= 300 where id in <foreach collection= "array" open= "(" separator= "," close= ")" item= "item" index= "index" > #{item} </foreach> </update> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
@Test public void foreachArrayTest(){ SqlSession sqlSession = sqlSessionFactory.openSession(); try { PostsMapper mapper = sqlSession.getMapper(PostsMapper. class ); int []ids={ 1 , 4 , 5 , 6 , 7 , 8 , 9 }; mapper.updateArray(ids); sqlSession.commit(); } catch (Exception e){ e.printStackTrace(); } finally { sqlSession.close(); } } |
1
2
3
4
5
6 |
<select id= "getByMap" resultType= "Posts" > select * from posts where title=#{title} and id in <foreach collection= "ids" item= "id" index= "index" open= "(" close= ")" separator= "," > #{id} </foreach> </select> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
@Test public void foreachMapTest(){ SqlSession sqlSession = sqlSessionFactory.openSession(); try { PostsMapper mapper = sqlSession.getMapper(PostsMapper. class ); int []ids={ 1 , 4 , 5 , 6 , 7 , 8 , 9 }; Map<String, Object> map= new HashMap<String, Object>(); map.put( "ids" , ids); map.put( "title" , "auto set元素生成的sql" ); Posts post=mapper.getByMap(map); System.out.println(post); //sqlSession.commit(); } catch (Exception e){ e.printStackTrace(); } finally { sqlSession.close(); } } |