MyBatis and Spring Integration (profile-based)

Source: Internet
Author: User

This article mainly introduces how to integrate MyBatis and spring together, I use mybatis3.05 + spring3.1.0m2, using DBCP as the database connection pool.

1. Writing The data Access Interface (Userdao.java)

Package com.mybatis;
Public interface Userdao {
public int countall ();
}

2. Writing the Data Provider mapping file (Userdaomapper.xml)

<?xml version= "1.0" encoding= "UTF-8"?>  

<mapper namespace= "Com.mybatis.UserDao" >
<select id= "Countall" resulttype= "int" >
Select COUNT (*) c from user;
</select>
</mapper>

3. Writing the MyBatis configuration file (mybatis-configuration.xml)

<?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>
<mappers>
<mapper resource= "Com/mybatis/userdaomapper.xml"/>
</mappers>
</configuration>

4. Writing the service Layer Interface (Userservice.java)

Package com.mybatis;

Public interface UserService {
public int countall ();
}

5. Writing the service Layer Implementation Code (USERSERVICEIMPL.JAVA)

Package com.mybatis;

public class Userserviceimpl implements UserService {
Private Userdao Userdao;

Public Userdao Getuserdao () {
return Userdao;
}
public void Setuserdao (Userdao Userdao) {
This.userdao = Userdao;
}

public int Countall () {
return This.userDao.countAll ();
}
}

6. Writing the spring configuration file (applicationcontext.xml)

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>
<property name= "url" value= "Jdbc:mysql://localhost:3306/hlp?useunicode=true&amp;characterencoding=utf-8 &amp;zerodatetimebehavior=converttonull "></property>
<property name= "username" value= "root" ></property>
<property name= "password" value= "1234" ></property>
<property name= "maxactive" value= "></property>"
<property name= "Maxidle" value= "></property>"
<property name= "maxwait" value= "></property>"
<property name= "Defaultautocommit" value= "true" ></property>
</bean>

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "configlocation" value= "Classpath:mybatis-configuration.xml" ></property>
<property name= "DataSource" ref= "DataSource"/>
</bean>

<bean id= "Userdao" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= "Mapperinterface" value= "Com.mybatis.UserDao" ></property>
<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory" ></property>
</bean>

<bean id= "UserService" class= "Com.mybatis.UserServiceImpl" >
<property name= "Userdao" ref= "Userdao" ></property>
</bean>

</beans>

7. Test code (USERSERVICETEST.JAVA)

Package com.mybatis;

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


public class Userservicetest {

@Test
public void Userservicetest () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
UserService UserService = (userservice) context.getbean ("UserService");
System.out.println (Userservice.countall ());
}
}

  

Appendix: Libraries that need to be imported

MyBatis and Spring Integration (profile-based)

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.