Spring (12) Spring Integration MyBatis

Source: Internet
Author: User

First, the first step depends on:

<!--mybatis+spring Integration--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!--Spring Integrated Javaweb Package--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--mybatis Jar Pack--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<!--jstl Expressions--
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!--Spring Integrated Javaweb Package--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>

2. Create a database: Make a simple add:

You have to create two JSPs.

3. Write Dao,service,entity,servlet

Implementation class

DAO layer

Service

Then there's the servlet.

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Request.setcharacterencoding ("Utf-8");
String name = Request.getparameter ("BookName");
String author = request.getparameter ("Bookauthor");
String Bookprice = Request.getparameter ("Bookprice");
Integer Price = Integer.parseint (Bookprice);

Book book = new book ();
Book.setbookname (name);
Book.setbookauthor (author);
Book.setbookprice (price);

ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext18ssm.xml");
ApplicationContext context = Webapplicationcontextutils.getrequiredwebapplicationcontext (This.getServletContext () );
Ibookservice service= (Ibookservice) Context.getbean ("Bookservice");
try {
int count= service.addbook (book);
if (count>0) {
Request.getrequestdispatcher ("/index.jsp"). Forward (Request,response);
}else {
Response.sendredirect ("/addbook.jsp");
}
} catch (Exception e) {
E.printstacktrace ();
}


}

protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
DoPost (Request,response);
}

Last configuration file

<?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:context= "Http://www.springframework.org/schema/context"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx.xsd
">

<bean id= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" >
<property name= "Driverclassname" value= "${jdbc.driver}" ></property>
<property name= "url" value= "${jdbc.url}" ></property>
<property name= "username" value= "${jdbc.username}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
</bean>

<!--2. jdbc.properties file is recognized--
<context:property-placeholder location= "Classpath:jdbc.properties" ></context:property-placeholder>

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


<!--3.dao-->
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" Value= "CN. Spring.t22ssm.dao "></property>
</bean>


<!--4.service-->
<bean id= "Bookservice" Class= "CN. Spring.t22ssm.service.IBookServiceImpl ">
<property name= "Bookdao" ref= "Ibookdao" ></property>
</bean>

<!--transaction: Transaction Manager--
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>

<tx:annotation-driven transaction-manager= "TransactionManager" ></tx:annotation-driven>






</beans>


Spring (12) Spring Integration MyBatis

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.