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>



原文地址:https://www.cnblogs.com/a157/p/8595548.html

时间: 2024-08-03 09:48:21

Spring(十二)Spring整合Mybatis的相关文章

Spring boot 学习笔记 (二)- 整合MyBatis

Spring boot 学习笔记 (二)- 整合MyBatis Spring Boot中整合MyBatis,并通过注解方式实现映射. 整合MyBatis 以Spring boot 学习笔记 (一)- Hello world 为基础项目,在pom.xml中添加如下依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter&l

Spring MVC(二)--Spring MVC登陆实例

本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登陆页 一.配置web.xml 创建好WEB项目之后的第一步就是配置web.xml文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" xmlns=&

springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件

整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的项目mybatis-spring-boot-start.对原理感兴趣可以研究2一下,这里只叙述步骤. 引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybati

Spring.NET教程(二十二)整合Quartz.NET(应用篇)

你曾经需要应用执行一个任务吗?这个任务每天或每周星期二晚上11:30,或许仅仅每个月的最后一天执行.一个自动执行而无须干预的任务在执行过程中如果发生一个严重错误,应用能够知到其执行失败并尝试重新执行吗?你和你的团队是用.net编程吗?如果这些问题中任何一个你回答是,那么你应该使用Quartz.NET调度器. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联.整合了 Quartz.NET的应用程序可以重用来自不同事件

java B2B2C电子商务平台分析之十二-----Spring Cloud Sleuth

一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三 二.服务追踪分析 微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败.随着业务的不断扩张,服务之间互相调用会越来越复杂. 三.术语 Spr

Spring(十二)使用Spring的xml文件配置方式实现AOP

配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.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" xmlns:context=&

十二.Spring AOP面向切面编程

什么是AOP? AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP的第一个案例 要求:使用AOP实现日志记录功能,核心模块和增强单独开发,运行时组装 1.创建接口HelloDao.HelloService. 创建接口类HelloDaoImpl.HelloServiceImpl并实现dao层接口 public interface IHolleDao { public void print

spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式

Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. Zuul大部分功能都是通过过滤器来实现的,这些过滤器类型对应于请求的典型生命周期. PRE: 这种过滤器在请求被路由之前调用.我们可利用这种过滤器实现身份验证.在集群中选择请求的微服务.记录调试信息等. ROUTING:这种过滤器将请求路由到微服务.这种过滤器用于构建发送给微服务的请求,并使用Apa

spring boot(二): spring boot+jdbctemplate+sql server

前言 小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致. 在spring boot中使用jdbc 连接sql server数据只需要引入两个jar:spring-boot-starter-jdbc.spring-boot-starter-data-jpa 项目结构 和上篇spring boot入门保持相同 pom.xml <dependencies> <

Spring学习六、AOP与整合Mybatis

十一.AOP AOP(Aspect Oriented Programming) 意为:面向切面编程 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的一种延续,是软件开发的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型. 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高开发效率. AOP的作用及优势 作用 程序运行期间,不修改源码对已有方法进行增强 优势 减少重复代码