Related articles
MyBatis series summary: MyBatis series
preface
-
Annotations are used more and more frequently in our work. Whether learning Spring or SpringBoot, annotation development is essential.
-
We need to learn how to use annotation development in Mybatis.
-
Here’s what the official document said:
- Using annotations to map simple statements will make your code look cleaner, but for slightly more complex statements, Java annotations will not only fail, but will make your complex SQL statements even more confusing. Therefore, if you need to do something very complex, it is best to use XML to map statements.
- It’s up to you and your team to decide how you want to configure the mapping and whether or not you want to unify the form of the mapping statement definition. In other words, never get stuck in one way, you can easily migrate and switch between annotation-based and XML-based statement mapping.
-
For simple statements we can use annotations, and for complex statements we can use XML statement mappings. Of course, both ways can exist at the same time!
Annotations implement CRUD
-
Prerequisites: You need to configure the mapper
-
<mappers> <package name="dy.mapper"/> </mappers> Copy the code
-
①, select (select)
-
Add annotated SQL in mapper
-
public interface UserMapper { @Select("select * from user where id = #{id}") List<User> getUserInfo(@Param("id") Integer id); }Copy the code
-
-
Test
-
@Test public void selectUser() { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> users = mapper.getUserInfo(30); for (User map: users){ System.out.println(map); } session.close(); } Copy the code
-
-
The execution result
②, add (insert)
-
Add annotated SQL in mapper
-
public interface UserMapper { @Insert("insert into user(name,age,`like`)values(#{name},#{age},#{like} )") Integer addUserInfo(User user); } Copy the code
-
-
Test
-
@Test public void addUserInfo() { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User User = new User(); User. Elegantly-named setName (" big fish "); user.setAge(25); User. SetLike (" fish "); Integer statusNum = mapper.addUserInfo(user); System.out.println(statusNum); session.commit(); Session.close (); }Copy the code
-
-
The execution result
③ update
-
Add annotated SQL in mapper
-
public interface UserMapper { @Update("update user set name = #{name},age = #{age},`like` = #{like} where id = #{id}") Integer updateUserInfo(User user); } Copy the code
-
-
Test
-
@Test public void updateUserInfo() { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User User = new User(); user.setId(43); User.setname (" new big fish "); user.setAge(23); User.setlike (" Good luck, chicken tonight!" ); Integer statusNum = mapper.updateUserInfo(user); System.out.println(statusNum); session.commit(); Session.close (); }Copy the code
-
-
The execution result
4, delete (delete)
-
Add annotated SQL in mapper
-
public interface UserMapper { @Delete("delete from user where id = #{id}") Integer deleteUserInfo(int id); } Copy the code
-
-
Test
-
@Test public void deleteUserInfo() { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); Integer statusNum = mapper.deleteUserInfo(43); System.out.println(statusNum); session.commit(); Session.close (); }Copy the code
-
-
The execution result
(5) Automatically commit transactions
-
Have you noticed that, in addition to query, other operations need to commit transactions!
-
MyBatis supports automatic commit transactions
-
Remember our MybatisUtils class? Don’t remember to look back at my previous articles!
-
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "ContextAplication.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); }} public static SqlSession getSession(){} No manual submission / / but is not recommended, proposals are submitted manually commit return sqlSessionFactory. OpenSession (true); }}Copy the code
-
Remove the commit transaction code, let’s test whether it works!
-
@Test public void addUserInfo() { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User User = new User(); User.setname (" I'm testing committed transactions "); user.setAge(25); User.setlike (" the transaction has been cancelled "); Integer statusNum = mapper.addUserInfo(user); System.out.println(statusNum); //session.com MIT (); Session.close (); }Copy the code
-
-
Execution result:
-
Why is it not recommended to set up auto-commit transactions?
- This is not used very often. If you perform one CRUD operation on the database at a time, you can use this. If you interact with the database multiple times in a method, if each connection is in a separate connection, the transaction is out of control!
- Here do not explain in detail why not control, such as the basic series of articles written, will be out of MyBatis source code series of articles! When the time comes to explain in detail, here only to explain the use, will use first! Find out why! Why can use like this!
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah