Spring3+mybatis3+mysql

Source: Internet
Author: User

Play a bit mybatis, the online source is not very clear. Wrote it myself. Hope to be useful to everyone (spring3+mybatis3+mysql)

Entity class:

  package   org.my.entity;   Public   class      User {  private   Integer ID;      private   String name;      Private   Integer age;      public   Integer getId () {  return   ID;       public   void   setId (Integer id) {  this . id =  ID;      public   String getName () {  return   name;   public   void   setName (String name) {  this . Name = name;      public   Integer getage () {  return   age;   public   void   setage (Integer age) {  this . Age =     Age }}

Mapper

 Package Org.my.maper; Import Org.apache.ibatis.annotations.Param; Import Org.apache.ibatis.annotations.Select; Import Org.my.entity.User;  Public Interface Usermapper {    publicvoid  insertuser (user user);    @Select ("SELECT * from user WHERE id = #{id}")     Public user getUser (@Param ("id ") Integer ID);}

Dao

 Package Org.my.dao; Import Org.my.entity.User;  Public Interface Userdao {    public  user Getuserbyid (user user);}

Impl (DAO Implementation):

 Package Org.my.impl; Import Org.my.dao.UserDao; Import Org.my.entity.User; Import Org.my.maper.UserMapper;  Public class Implements Userdao {    private  usermapper usermapper;      Public void Setusermapper (Usermapper usermapper) {         this. Usermapper= usermapper;     }     @Override    public  user Getuserbyid (user user) {        return  Usermapper.getuser (User.getid ());}    }

Test class:

  package   org.my;   Import   Org.my.dao.UserDao;   Import   Org.my.entity.User;   Import   Org.my.impl.UserDaoImpl;   Import   Org.springframework.context.ApplicationContext;   Import   Org.springframework.context.support.ClassPathXmlApplicationContext;   Public   class   Test {  public   static   void   main (string[] args) {appli         Cationcontext ctx  =  new   classpathxmlapplicationcontext ( "Beans.xml" );        Userdao Userdao  = (Userdaoimpl) ctx.getbean ("Userdaoimpl" );        User User  =  new   user ();        User.setid ( 1 );        User u  =  Userdao.getuserbyid (user);    System.out.println (U.getname ()); }    }

Beans.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-b Eans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframewor K.org/schema/context/spring-context-2.5.xsd ">    <Context:property-placeholder Location= "Jdbc.properties" />    <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "${driver}" />        < Propertyname= "url"value= "${url}" />        < Propertyname= "username"value= "${username}" />        < Propertyname= "Password"value= "${password}" />    </Bean>    <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource" />    </Bean>    <BeanID= "Usermapper"class= "Org.mybatis.spring.mapper.MapperFactoryBean">        < Propertyname= "Mapperinterface"value= "Org.my.maper.UserMapper" />        < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory" />    </Bean>    <BeanID= "Userdaoimpl"class= "Org.my.impl.UserDaoImpl">        < Propertyname= "Usermapper"ref= "Usermapper" />    </Bean></Beans>

Jdbc.proerties:

driver=com.mysql.jdbc.Driverusername=Rootpassword=rooturl=jdbc\:mysql\:// localhost\:3306/test  

Next is the project:

Jar Package:

I hope you follow this configuration, can be successfully configured

Related Article

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.