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