Additional operations on single tables
Earlier we saw how to build mybatis framework and query operations, here we talk about how to use mybatis to increase user operations. First, insert method is added to user.xml file. The code is as follows
Then write a test method, code as follows:<!-- Add user --> <insertid="insertUser"parameterType="cn.itcast.mybatis.po.User"> insert into user(username,birthday,sex,address) <!--There#The value in {} must be the same as the attribute name in the user class. It is recommended to paste it directly - >. values(#{username},#{birthday},#{sex},#{address}) </insert>
Note: It's important to emphasize that mybatis automatically opens things because it uses jdbc defaults, but it doesn't know when to submit them, so it's necessary to submit things manually.// Adding user information @Test publicvoid testInsert() { // Database session instance SqlSession sqlSession = null; try { // Create database session instance sqlSession sqlSession = sqlSessionFactory.openSession(); // Adding user information User user = new User(); user.setUsername("Zhang Xiaoming"); user.setAddress("Zhengzhou, Henan"); user.setSex("1"); sqlSession.insert("test.insertUser", user); //Submit a transaction,Manual submission is required here. sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } }
A more careful classmate may have noticed that we did not write the value of id in our insert statement. This requires that our database grow by itself, but there is still a problem: the users we add in are inconsistent with the user information in our memory because our users in our memory do not have primary keys. How to solve this problem? We need to concentrate on these configurations in user.xml:
In this case, selectKey can return the primary key automatically generated by the database, and then assign our primary key to our user object automatically, so that the data in the database and system memory will be consistent.<insert id="insertUser" parameterType="cn.itcast.pojo.User"> <!-- select LAST_INSERT_ID() mybatis Function is the primary key for querying the last added data id keyProperty:Put the data queried by this database function into the input parameter User Object id Save in attributes order: current mysql Function relative to insert Execution order of statements, stay insert Pre execution is before, stay insert Post execution is after resultType: keyProperty Return specified in id Types of attributes --> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID() </selectKey> insert into user (username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address}) </insert>
Some students may say that if I don't use primary keys to grow by myself, what should I do with uuid? Here's how mybatis generates primary keys through uuid. In fact, it is very simple, that is, to put our.
change intoselect LAST_INSERT_ID()
Then change after to before. The code is as follows:select uuid()
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<selectKey resultType="java.lang.String" order="BEFORE"
keyProperty="id">
select uuid()
</selectKey>
insert into user(id,username,birthday,sex,address)
values(#{id},#{username},#{birthday},#{sex},#{address})
</insert>
//Note: The order used here is "BEFORE"
Delete operation of single table
It's easier to delete users, so I just paste my code here.
1.user.xml file
<!-- delete user --> <deleteid="deleteUserById"parameterType="int"> delete from user where id=#{id} </delete>
2. Test code
@Test publicvoidtestDelete() { // Database session instance SqlSession sqlSession = null; try { // Create database session instance sqlSession sqlSession = sqlSessionFactory.openSession(); // delete user sqlSession.delete("test.deleteUserById",18); // Submission of affairs sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } }
Modification of a single table
Modifying the code is relatively simple, paste the source code directly:
1.user.xml file
2. Testing procedures<updateid="updateUser"parameterType="cn.itcast.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update>
@Test publicvoidtestUpdate() { // Database session instance SqlSession sqlSession = null; try { // Create database session instance sqlSession sqlSession = sqlSessionFactory.openSession(); // Adding user information User user = new User(); user.setId(16); user.setUsername("Zhang Xiaoming"); user.setAddress("Zhengzhou, Henan"); user.setSex("1"); user.setPrice(1999.9f); sqlSession.update("test.updateUser", user); // Submission of affairs sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } }