MyBatis Learning (5) integration with SPRING3

Source: Internet
Author: User

In this series of articles, there are examples of purely using mybatis to connect to a database, and then to make additional deletions, as well as multi-table federated queries, but in real-world projects, spring is often used to manage datasource and so on. Take advantage of spring's interface-based programming, as well as AOP, the IOC brings convenience. There are a lot of similarities between managing MyBatis with spring and managing hibernate. Today's focus is on data source management and bean configuration.

You can download the source code, compared to see, the source is not with the jar package, too big, space is limited. Yes, you can see which jar packages are used, source code at the end of this article.

1. First make a little change to the previous engineering structure, set up the folder config under the Src_user source code directory, and move the original MyBatis profile Configuration.xml to this folder, and set up the Spring in Config home folder. Configuration file: Applicationcontext.xml, the most important configuration in this configuration file:
Program code
<!--This example uses the DBCP connection pool, you should pre-copy the DBCP jar package into the project's Lib directory. -

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>
<property name= "url" value= "Jdbc:mysql://127.0.0.1:3306/mybatis?characterencoding=utf8"/>
<property name= "username" value= "root"/>
<property name= "password" value= "password"/>
</bean>

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<!--DataSource property specifies the connection pool to use--
<property name= "DataSource" ref= "DataSource"/>
<!--Configlocation property specifies the core configuration file for MyBatis--
<property name= "configlocation" value= "Config/configuration.xml"/>
</bean>

<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<!--Sqlsessionfactory property specifies the Sqlsessionfactory instance to use--
<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/>
<!--The Mapperinterface property specifies the Mapper interface for implementing this interface and generating mapper objects--
<property name= "Mapperinterface" value= "Com.yihaomen.mybatis.inter.IUserOperation"/>
</bean>


[B] The focus of this is Org.mybatis.spring.SqlSessionFactoryBean and Org.mybatis.spring.mapper.mapperfactorybean[b]. Spring's interface, and produces the object. You can view the Mybatis-spring code in detail. (http://code.google.com/p/mybatis/), if only use, fixed mode, so configuration is good.

then write the test program
Program code
Package com.yihaomen.test;

Import java.util.List;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import com.yihaomen.mybatis.inter.IUserOperation;
Import com.yihaomen.mybatis.model.Article;
Import Com.yihaomen.mybatis.model.User;


public class Mybatissprinttest {

private static ApplicationContext ctx;

Static
{
CTX = new Classpathxmlapplicationcontext ("Config/applicationcontext.xml");
}

public static void Main (string[] args)
{
Iuseroperation mapper = (iuseroperation) ctx.getbean ("Usermapper");
Test id=1 user queries, depending on the situation in the database, can be changed to your own.
System.out.println ("Get user id=1 User Information");
User user = Mapper.selectuserbyid (1);
System.out.println (User.getuseraddress ());

Get a list of articles to test
System.out.println ("Get a list of all articles with User ID 1");
List<article> articles = Mapper.getuserarticles (1);

for (article article:articles) {
System.out.println (article.getcontent () + "--" +article.gettitle ());
}

}


}



can be run to get the corresponding results.
Engineering Drawing:

the jar packages used, such as:

MyBatis Learning (5) integration with SPRING3

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.