MyBatis Getting Started (ii)--Accessing the database by means of an interface

Source: Internet
Author: User

First, establish the interface Usermapper.java
publicinterface UserMapper {    publicgetAllUser();    publicgetUserById(String userId);    publicintinsert(User user);    publicintupdate(User user);    publicintdelete(User user);}
Two modifications Usermapper.xml
<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="Com.elements.user.mapper.UserMapper">Note that this differs from the original way here must and your interface path + name has been (excluding. Java)<resultmap type="Com.elements.user.model.User" id="UserMap" >        <ID Column="userid" property ="userid"/>        <result column= "UserName" property ="UserName"/>        <result column= "useremail" property ="UserEmail"/>     </resultmap>    <select id="Getalluser" resultmap="UserMap"> SELECT * FROM Mybatis.user</Select>    <select id="Getuserbyid" resultmap="UserMap"  ParameterType="String">SELECT * from Mybatis.user where userid= #{userid}</Select>    <insert id="Insert" parametertype=" Com.elements.user.model.User ">Insert into Mybatis.user (UserName, UserEmail) VALUES (#{username}, #{useremail})</Insert>    <update id="Update" parametertype="Com.elements.user.model.User" >Update Mybatis.user set Username=#{username}, Useremail=#{useremail} where userid= #{userid}</Update>    <delete id="Delete" parametertype="String"> Delete mybatis.user where userid= #{userid}</Delete></mapper>
Three, mybatis-config.xml configuration

This becomes the class of the interface

<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration>    <properties Resource="jdbc.properties" />    <environments Default="Dev">        <environment ID="Dev">            <transactionmanager type="JDBC" />            <dataSource type="Pooled">                < property name="Driver" value="${driver}" />                 < property name="url" value="${url}" />                < property name="username" value="${username}" />                < property name="password" value="${password}" / >            </dataSource>        </Environment>    </environments>    <mappers>        <mapper class="Com.elements.user.mapper.UserMapper"/>    </mappers></configuration>
Iv. Test class
public  class  TestUser {@Test public  void   Testselectall  () throws IOException {String resource =  "Mybatis-config.xml"  ; InputStream InputStream = resources.getresourceasstream (Resource); Sqlsessionfactory sqlsessionfactory = new  Sqlsessionfactorybuilder (). Bui LD (InputStream); sqlsession session = Sqlsessionfactory.opensession (); try  {Usermapper user = (Usermapper) session.getmapper (Usermapper.class); List<user> userlist = User.getalluser (); System. out . Print (userlist); } finally  {session.close (); } }}

Project code Download: Http://pan.baidu.com/s/1c12EDaC

Five, not mybatis-config.xml configuration mode

 Public classTestUser {@Test Public void Testselectall() throws IOException {String resource ="Mybatis-config.xml";        InputStream InputStream = resources.getresourceasstream (Resource); Sqlsessionfactory sqlsessionfactory =NewSqlsessionfactorybuilder (). Build (InputStream); sqlsession session = Sqlsessionfactory.opensession ();if(!session.getconfiguration (). Getmapperregistry (). Hasmapper (Usermapper.class)) session.getconfiguration (). AddMap Per (Usermapper.class);//Get the session configuration file here and see if our interface is registered in Mapperregistry        //Add our interface if not registered        Try{Usermapper user = (Usermapper) session.getmapper (Usermapper.class);            List<user> userlist = User.getalluser (); System. out. print (userlist); }finally{Session.close (); }    }}

Project code Download: Http://pan.baidu.com/s/1slBrQ0X

MyBatis Getting Started (ii)--Accessing the database by means of an interface

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.