Summary of small cases of spring+mybatis J2SE

Source: Internet
Author: User

Package Dao;import Java.util.list;import model. User;public interface Userdao {public User getUser (int i);p ublic list<user> getalluser ();p ublic int Insertuser ( User u);p ublic int UpdateUser (user u);p ublic int deleteuser (int i);}

<?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= "DAO. Userdao "> <select id=" getUser "parametertype=" int "resulttype=" User ">select * from T_user where id=#{id}</ Select><select id= "Getalluser" resulttype= "User" >select * from T_user</select><delete id= " DeleteUser "parametertype=" int ">delete from T_user where id=#{id}</delete><update id=" UpdateUser " Parametertype= "User" >update T_user set Username=#{username}, Password=#{password} where id=#{id}</update> <insert id= "Insertuser" parametertype= "User" >insert into T_user (Username,password) VALUES (#{username},#{ Password}) </insert></mapper>

Package Model;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.Component ; @Component @scope ("prototype") public class User {private int id;private string Username;private string password;public User () {super ();} Public User (string Username, string password) {super (); this.username = Username;this.password = password;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

Package Service;import Java.util.list;import model. User;public interface Iusersrv {public User getUser (int i);p ublic list<user> getalluser ();p ublic int Insertuser ( User u);p ublic int UpdateUser (user u);p ublic int deleteuser (int i);}

Package Service;import Java.util.list;import Javax.annotation.resource;import Org.springframework.stereotype.component;import Org.springframework.transaction.annotation.transactional;import Dao. Userdao;import model. User; @Component @transactionalpublic class Usersrv implements Iusersrv {@Resourceprivate  Userdao userdao;@ Overridepublic User getUser (int i) {//TODO auto-generated method stub return Userdao.getuser (i);} @Overridepublic list<user> Getalluser () {//TODO auto-generated method stub return Userdao.getalluser ();} @Overridepublic int Insertuser (User u) {//TODO auto-generated method stub return Userdao.insertuser (U);} @Overridepublic int UpdateUser (User u) {//TODO auto-generated method stub return Userdao.updateuser (U);} @Overridepublic int deleteuser (int i) {//TODO auto-generated method stub return Userdao.deleteuser (i);}}

Package Test;import Javax.annotation.resource;import Org.springframework.stereotype.component;import model. User;import service. Iusersrv;import util. Beanhelper; @Componentpublic class Test {@Resourceprivate User user;public static void Main (string[] args) {iusersrv UserS RV = (iusersrv) beanhelper.getbean ("Usersrv"); System.out.println (Usersrv.getuser (1). GetUserName ()); Test T = (test) beanhelper.getbean ("Test"), T.user.setusername ("DD"), T.user.setpassword ("xxx"); System.out.println (Usersrv.insertuser (T.user));}}

Package Util;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Beanhelper {private BeanHelper () {} private static ApplicationContext AC = new Classpathxmlapplicationcontext ("Spring.xml"); public static Object Getbean (String beanname) {return Ac.getbean (beanname);}}

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd "><!--1. Data Source: Drivermanagerdatasource--><context:annotation-config></context:annotation-config><bean ID = "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" ><property name= " Driverclassname "value=" Com.mysql.jdbc.Driver "/><property name=" url "value=" jdbc:mysql://localhost:3306/db_book?useunicode=true&characterencoding=utf-8 "/><property name=" username "value=" root "/>< Property name= "Password" value= "root"/></bean><!--2. MyBatis's sqlsession factory: Sqlsessionfactorybean datasource/typealiasespackage--><bean id= "SqlSessionFactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" ><property name= "DataSource" ref= "DataSource"/>< Property Name= "Typealiasespackage" value= "model"/></bean><!--3. MyBatis automatic Scan Load SQL mapping file: mapperscannerconfigurer sqlsessionfactory/basepackage--><bean id= "config" class= " Org.mybatis.spring.mapper.MapperScannerConfigurer "><property name=" basepackage "value=" DAO "/>< Property Name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/></bean><!--4. Transaction management: Datasourcetransactionmanager--><bean id= "manager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "><property name=" datasource "ref=" DataSource "/></bean><!--5. Use declarative transactions--><tx:annotation-driven transaction-manager= "Manager"/><!--6. Scan loads the bean under each package--><context:component-scan base-package= "Service,model,test" ></context:component-scan ></beans>

Summary of small cases of spring+mybatis J2SE

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.