Java Framework--spring+mybatis Annotation Integration

Source: Internet
Author: User

 Package Cn.sxt.dao; Import java.util.List; Import Org.apache.ibatis.annotations.Select; Import Cn.sxt.vo.User;  Public Interface Usermapper {        @Select ("SELECT * from User")    public list<user>  Selectuser (); }
 Package Cn.sxt.service; Import java.util.List; Import Cn.sxt.vo.User;  Public Interface UserService {    public list<user> selectuser ();} 
 PackageCn.sxt.service.impl;Importjava.util.List;ImportCn.sxt.dao.UserMapper;ImportCn.sxt.service.UserService;ImportCn.sxt.vo.User; Public classUserserviceimplImplementsuserservice{PrivateUsermapper Usermapper =NULL;  Public voidSetusermapper (Usermapper usermapper) { This. Usermapper =Usermapper; } @Override PublicList<user>Selectuser () {System.out.println (usermapper); returnUsermapper.selectuser (); }}
 Packagecn.sxt.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCn.sxt.service.UserService; Public classTest { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); UserService UserService= (UserService) context.getbean ("UserService");    System.out.println (Userservice.selectuser (). Size ()); }}
 PackageCn.sxt.vo; Public classUser {Private intID; PrivateString name; PrivateString pwd;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getpwd () {returnpwd; }     Public voidsetpwd (String pwd) { This. PWD =pwd; }}
<?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:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--configuration Data source-<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/orcl "/> <proper Ty name= "username" value= "root"/> <property name= "password" value= "123456"/> </bean> <!--sound Ming transaction Configuration Start-<!--Configure transaction manager-<bean id= "Txmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= " DataSource "/> </bean> <!--configuring transaction notifications-<tx:advice id=" Txadvice "transaction-manager=" Txmanager "&        Gt <tx:attributes> <!--Configure which methods use what transactions, configure the propagation characteristics of a transaction--<tx:method name= "Add" propagation= " REQUIRED "/> <tx:method name=" Insert "propagation=" REQUIRED "/> <tx:method name=" Update "p ropagation= "REQUIRED"/> <tx:method name= "delete" propagation= "REQUIRED"/> <tx:method nam  E= "remove*" propagation= "REQUIRED"/> <tx:method name= "Get" read-only= "true"/> <tx:method Name= "*" propagation= "REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> &lt ; Aop:pointcut expression= "Execution (* cn.sxt.service.impl.*.* (..))" id= "Pointcut"/> <aop:advisor advice-ref= " Txadvice "Pointcut-ref= "Pointcut"/> </aop:config> <!--declarative transaction configuration ended--<!--configuration Sqlsessionfactory--<bean I  D= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "DataSource"/> &LT;/BEAN&G    T <bean id= "Usermapper"class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property name= "mapperinterface" value= "    Cn.sxt.dao.UserMapper "/> <property name=" sqlsessionfactory "ref=" Sqlsessionfactory "/> </bean> <bean id= "UserService"class= "Cn.sxt.service.impl.UserServiceImpl" > <property name= "usermapper" ref= "Usermapper"/> </bean>&lt ;/beans>
<?xml version= "1.0" encoding= "UTF-8"? ><!  "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration >    <typeAliases>        < package name= "Cn.sxt.vo"/>    </typeAliases>        < Mappers>        < package name= "Cn.sxt.dao.UserMapper"/>    </mappers> </configuration >
/*Navicat MySQL Data transfersource server:testsource Server version:50520source host:localhost : 3306Source database:orcltarget Server type:mysqltarget server Version:50520file encoding:65001da te:2018-01-07 12:32:39*/SET foreign_key_checks=0;--------------------------------Table structure for' user '-- ----------------------------DROP TABLE IF EXISTS ' user '; CREATE TABLE ' user ' (' name ' varchar (10) DEFAULT NULL, ' id 'int(20) not NULL auto_increment, ' pwd ' varchar (30DEFAULT NULL, PRIMARY KEY (' id ')) ENGINE=innodb auto_increment=3 DEFAULT charset=UTF8;-- ------------------------------Records of User-- ----------------------------INSERT into ' user ' VALUES (' 澶 firmament 緺 ', ' 1 ', ' 4444 'INSERT into ' user ' VALUES (' 澶 firmament 緺 ', ' 2 ', ' 4444 ');

Printed: 9

Java Framework--spring+mybatis Annotation Integration

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.