Mybatis learning notes-curd (based on configuration files)

Source: Internet
Author: User

User. Java entity class

public class User {private int id;private String username;private int age;//...}

Usermapper. xml

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. mybatis. test02.usermapper "> <! -- CRUD operation --> <insert id = "adduser" parametertype = "com. mybatis. test02.user "> insert into users (username, age) values (# {username}, # {age }) </Insert> <Delete id = "deleteuser" parametertype = "int"> Delete from users where ID =#{ ID} </delete> <update id = "updateuser" parametertype = "com. mybatis. test02.user "> Update users set username =#{ username }, age = # {age} Where id = # {ID} </update> <select id = "getuser" parametertype = "int" resulttype = "com. mybatis. test02.user "> select * from users where ID =#{ ID} </SELECT> <select id =" getallusers "resulttype =" com. mybatis. test02.user "> select * from users </SELECT> </mapper>


Mybatisutil. Java

public class MybatisUtil {public static SqlSessionFactory getFactory() {String resource = "conf.xml";InputStream inputStream = Test.class.getClassLoader().getResourceAsStream(resource);SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);return factory;}}


Test Method

@ Testpublic void testadd () {sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); // set to automatically submit user user2 = new user (); user2.setusername ("SSDD"); user2.setage (22); string S = "com. mybatis. test02.usermapper. adduser "; int insert = session. insert (S, user2); // Number of affected records // session. commit (); system. out. println (insert); Session. close () ;}@ testpublic void testdelete () {sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); // set to automatically submit string S = "com. mybatis. test02.usermapper. deleteuser "; int Delete = session. delete (s, 1); // session. commit (); system. out. println (delete); Session. close ();} // updateuser @ testpublic void testupdate () {sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); string S = "com. mybatis. test02.usermapper. updateuser "; user = new user (); User. setid (3); User. setusername ("umgsai"); User. setage (24); int update = session. update (S, user); system. out. println (update); Session. close () ;}@ testpublic void testselect () {sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); string S = "com. mybatis. test02.usermapper. getuser "; user = session. selectone (S, 5); system. out. println (User); Session. close () ;}@ testpublic void testselectall () {sqlsessionfactory factory = mybatisutil. getfactory (); sqlsession session = factory. opensession (true); string S = "com. mybatis. test02.usermapper. getallusers "; List <user> Users = session. selectlist (s); Session. close (); system. out. println (users );}



This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1559082

Mybatis learning notes-curd (based on configuration files)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.