Spring(十二)Spring整合Mybatis

首先第一步引依赖:

<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!-- Spring整合JavaWeb的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--mybatis jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<!--jstl表达式-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Spring整合JavaWeb的包 -->
<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.创建数据库:做一个简单的添加吧:

还得创建两个jsp

 

 

 

3.写dao,service,entity,servlet

    实现类

  dao层  

 

service:

然后就是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);
}

最后配置文件

<?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文件-->
<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>

<!--事务:事务管理器-->
<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>



 

 

 

 

 

 

 

 

 


posted @ 2018-03-18 15:15  Gtr_Right  阅读(132)  评论(0编辑  收藏  举报