1. Set log
Logs can facilitate our troubleshooting. They are not enabled by default. They need to be set in the core configuration file.
name and value should be consistent with the official documents!
1.1 default log
<!--Set log default configuration--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
1.2LOG4J
- Import the dependency of log4j (pom.xml)
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency>
- mybatis-config.xml core configuration file
<!--Set log--> <settings> <setting name="logImpl" value="LOG4J"/> </settings>
- log4j.properties (in the resources package)
#Output the log information with the level of DEBUG to the two destinations of console and file. The definitions of console and file are in the following code log4j.rootLogger=DEBUG,console,file #Settings related to console output log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n #Settings related to file output log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/ZXF.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n #Log output level log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
After this setting, the logs will be output at the same time the next time the console is output.
2. Pagination
Paging operation is required to master.
2.1 limit paging
Interface
//Paging get user List<User> getUserLimit(Map<String,Object> map);
xml configuration file
<select id="getUserLimit" parameterType="map" resultType="user"> select * from mybatis.user limit #{startindex},#{endindex}; </select>
test
@Test public void getUserLimit(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Object> map = new HashMap<>(); map.put("startindex",0); map.put("endindex",3); List<User> userLimit = mapper.getUserLimit(map); for (User user : userLimit) { System.out.println(user); } sqlSession.close(); }
2.2 rowboundaries paging
Interface
//rowBounds paging List<User> getUserRowBounds();
xml configuration file
<select id="getUserRowBounds" resultType="user"> select * from mybatis.user; </select>
test
@Test public void getUserRowBounds(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); RowBounds rowBounds = new RowBounds(0, 2); List<User> user = sqlSession.selectList("com.ZXF.dao.UserMapper.getUserRowBounds", null, rowBounds); for (User user1 : user) { System.out.println(user1); } sqlSession.close(); }
In the test classes of the two methods, we can see that the methods called by sqlsession are different. The methods in rowboundaries are not commonly used now, and the SQL statements should be spliced when using rowboundaries paging. Personally, I am used to using the limit paging method.
3.lombok
lombok is a tool that can make us lazy.
What lombok said on its official website:
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
When we are faced with some repetitive and lengthy code with little nutritional value, we will get tired, and lombok will help us reduce these codes.
usage method
- Download the lombok plug-in in settings.
- Import dependencies in the project you want to use
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency>
- example
@Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private int tid; }
These are just a few of lombok. You can see the official website for more information https://projectlombok.org/
4. Development using annotations
For mybatis, some simple functions can be developed using annotations, but some complex SQL statements still need to be implemented through xml!
When developing with annotations, the core configuration file must be registered with class
<mappers> <!--Develop binding interfaces using annotations--> <mapper class="com.ZXF.dao.UserMapper"/> </mappers>
CRUD with annotations
Interface
//Find users by id @Select("select * from user where id=#{id}") User getUserById(@Param("id") int id); //Insert user @Insert("insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})") int insertUser(User user); //Update user @Update("update user set name=#{name},pwd=#{pwd} where id=#{id}") int updateUser(User user); //delete user @Delete("delete from user where id=#{id}") int deleteUser(@Param("id") int id);
@The Param annotation must be used when multiple parameters need to be passed in, but one parameter can be used or not. However, in order to form a habit and troubleshoot later, it is recommended to use it when passing in one parameter.
test
@Test public void getUserById(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(5); System.out.println(user); sqlSession.close(); } @Test public void insertUser(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int i = mapper.insertUser(new User(6, "77", "1234567")); sqlSession.commit(); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(6,"77","1111111")); sqlSession.commit(); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(6); sqlSession.commit(); sqlSession.close(); }
This is the fourth of my mybatis self-study notes. If you haven't read my previous articles, you can see the links of the first three articles ↓
mybatis learning notes-03
mybatis learning notes-02
mybatis learning notes-01
This article ends here. Self study is not easy. If you don't write it in detail, you can read it in combination with the official documents or exchange it in the comment area. This article can be optimized. I hope the boss can give you advice.