Next, let's do another test to query the user's record based on the user name.
Similarly, now add a sql statement to userMapper.xml
<select id="selectUserByName" parameterType="java.lang.String" resultType="com.beyond.mybatis.po.User"> select * from user WHERE username like '%${value}%' </select>
Since the results of the query may return multiple items, we use selectList to query here.
/** * @author:kevin * @Description: Fuzzy query based on username * @Date:12:08 2018/3/24 */ @Test public void findUserByName() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //create session factory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //create session SqlSession sqlSession = factory.openSession(); //Operate the database through sqlsession List user = sqlSession.selectList("test.selectUserByName","李"); System.out.println(user); sqlSession.close(); }
output:
summary:
parameterType:
Specify the types of input parameters in the mapping file
resultType:
Specify the type of the returned result in the mapping file.
The difference between #{} and ${}
#{} represents a placeholder. When #{} receives a simple type, it can contain any parameters #{value}, #{id}, etc., receive the value of the pojo object, and read the attribute value in the formation through OGNL.
${} represents a splicing symbol, which will cause SQL injection, so it is not recommended to use it. When ${} receives a simple type, it can only contain value, ${value}.
selectOne queries a single record.
selectList queries multiple records.
Add user information.
<!-- add user parameterType: Specifies that the input parameter type is pojo --> <insert id="insertUser" parameterType="com.beyond.mybatis.po.User"> insert into user(id,username,birthday,sex,addr) VALUES (#{id},#{username},#{birthday},#{sex},#{addr}) </insert>
/** * @author:kevin * @Description: add user * @Date:12:08 2018/3/24 */ @Test public void insertUser() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //create session factory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //create session SqlSession sqlSession = factory.openSession(); //Operate the database through sqlsession //List user = sqlSession.selectList("test.selectUserByName","李"); User user = new User(); user.setUsername("Wang Xiaochuan"); user.setBirthday(new Date()); user.setSex("1"); user.setAddr("Chengdu, Sichuan"); sqlSession.insert("test.insertUser",user); // commit the transaction sqlSession.commit(); sqlSession.close(); }
View database after successful insertion
Auto-increment primary key return
MySQL auto-incrementing primary key, execute insert commit to automatically generate an auto-incrementing primary key. Get the auto-incrementing primary key of the record just inserted through the mysql function:
LAST_INSERT_ID(), called after insert.
The definition of insertUser needs to be modified as follows:
<insert id="insertUser" parameterType="com.beyond.mybatis.po.User"> <!-- Return the primary key of the inserted data to the user object SELECT LAST_INSERT_ID(): Get the primary key value of the record just inserted, only applicable to self-incrementing primary key keyProperty: which property of the object specified by parameterType is set to the primary key value of the query order: SELECT LAST_INSERT_ID() execution order, its execution order relative to insert --> <selectKey keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into user(id,username,birthday,sex,addr) VALUES (#{id},#{username},#{birthday},#{sex},#{addr}) </insert>
Execute the insert again:
This time the console prints out an id of 39.
Return of non-auto-incrementing primary key:
Such as the return of the uuid primary key
Need to modify the id type to varchar, and then get the uuid before insert
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> SELECT uuid() </selectKey>
delete users:
<!-- delete by id--> <delete id="deleteUserById" parameterType="int"> DELETE FROM USER WHERE id=#{id} </delete>
/** * @author:kevin * @Description: delete user information based on id * @Date:12:08 2018/3/24 */ @Test public void deleteUser() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //create session factory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //create session SqlSession sqlSession = factory.openSession(); //Operate the database through sqlsession sqlSession.delete("test.deleteUserById",39); // commit the transaction sqlSession.commit(); sqlSession.close(); }
Update user:
<!-- update user --> <update id="updateUser" parameterType="com.beyond.mybatis.po.User"> UPDATE USER SET username=#{username},birthday=#{birthday},sex=#{sex} WHERE id=#{id} </update>
/** * @author:kevin * @Description: update user * @Date:12:08 2018/3/24 */ @Test public void updateUser() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //create session factory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //create session SqlSession sqlSession = factory.openSession(); //Operate the database through sqlsession //List user = sqlSession.selectList("test.selectUserByName","李"); User user = new User(); user.setId(38); user.setUsername("Luo Feng"); user.setBirthday(new Date()); user.setSex("1"); user.setAddr("Chengdu, Sichuan"); sqlSession.update("test.updateUser",user); // commit the transaction sqlSession.commit(); sqlSession.close(); }At this point, the introductory procedure of Mybatis is almost over.