SpringMybatis 整合JavaWeb
需要用到新的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>01Mybatis</artifactId>
<groupId>cn.kitty</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Mybatis-SpringSSM</artifactId>
<packaging>war</packaging>
<name>Mybatis-Spring SSM Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<!--<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<!--织入-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!--配置c3p0-->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--Spring-jdbc需要的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!--mybatisSpring jar-->
<!--spring整合mybatis-->
<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--JavaEE的依赖-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<!-- Spring整合JavaWeb的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency> </dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
package cn.kitty.contrllor; public class Book {
private int bookid;
private String bookname;
private int bookprice ; public Book() {
} public Book(int bookid, String bookname, int bookprice) {
this.bookid = bookid;
this.bookname = bookname;
this.bookprice = bookprice;
} public int getBookid() {
return bookid;
} public void setBookid(int bookid) {
this.bookid = bookid;
} public String getBookname() {
return bookname;
} public void setBookname(String bookname) {
this.bookname = bookname;
} public int getBookprice() {
return bookprice;
} public void setBookprice(int bookprice) {
this.bookprice = bookprice;
}
}
IBookDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kitty.dao.IBookDao">
<insert id="addBook">
insert book (bookname,bookprice) VALUES (#{bookname},#{bookprice})
</insert>
</mapper>
IBookService
package cn.kitty.service; import cn.kitty.contrllor.Book; public interface IBookService {
public void addBook(Book book);
}
IBookServiceImpl
package cn.kitty.contrllor.service.impl; import cn.kitty.contrllor.controller.Book;
import cn.kitty.contrllor.dao.IBookDao;
import cn.kitty.contrllor.service.IBookService; public class BookServiceImpl implements IBookService {
private IBookDao dao;
public void addBook(Book book) {
dao.addBook(book);
} public IBookDao getDao() {
return dao;
} public void setDao(IBookDao dao) {
this.dao = dao;
}
}
BookServlet
package cn.kitty.servlet; import cn.kitty.contrllor.Book;
import cn.kitty.service.IBookService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String bookname=request.getParameter("bookname");
int bookprice=Integer.parseInt(request.getParameter("bookprice"));
Book book=new Book();
book.setBookname(bookname);
book.setBookprice(bookprice);
//调度service实现添加 01 ApplicationContext
//02程序一启动,在初始化ServletContext (Servlet上下文)application类型 应用程序在初始化
//application 我们可以开一个监听器,顺便的将Spring容器同时也给初始化
//01 ApplicaitionContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//02.WebApplicationContext context= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
WebApplicationContext context= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
IBookService bookService = (IBookService)context.getBean("bookService");
bookService.addBook(book);
request.getRequestDispatcher("/index.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request ,response);
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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
">
<!--01.识别jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///aount"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
<!--03.工厂配置-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--big config path-->
<property name="configLocation" value="classpath:Mybatis-config.xml"></property>
</bean>
<!--dao 实现类 映射文件的扫描器可以动态的在内存中构建接口的实现类,代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kitty.dao"></property>
</bean>
<!--service-->
<bean id="bookService" class="cn.kitty.service.impl.BookServiceImpl">
<property name="dao" ref="IBookDao"></property>
</bean>
<!--06.事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 07.AspectJ AOP 配置事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addBook" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--配置了切点Pointcut * *..service.*.*(..))-->
<aop:pointcut id="mypoint" expression="execution(* *..service.*(..))"/>
<!--顾问-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
</aop:config> </beans>
jdbc.perperties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///aount
jdbc.username=root
jdbc.password=
Mybatis-config.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>
<!--<typeAliases>
<package name="cn.kitty.controllerroller"></package>
</typeAliases>-->
<!-- <mappers>
<!–<package name="cn.kitty.dao"></package>–>
<mapper resource="cn/kitty/dao/IBookDao.xml"/>
</mappers>-->
</configuration>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>cn.kitty.servlet.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/BookServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>addBook.jsp</welcome-file>
</welcome-file-list>
</web-app>
addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加图书</title>
<form name="add" method="post" action="/BookServlet" >
图书名称:<input name="bookname"/>
图书价格:<input name="bookprice"/>
<input type="submit" value="添加">
</form>
</head>
<body> </body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>添加图书成功了</h2>
</body>
</html>
SpringMybatis 整合JavaWeb的更多相关文章
- mybatis快速入门(七)-spring-mybatis整合
今天写写spring-mybatis整合吧,先写个原始dao类的整合,下一节在写个动态代理的整合,我就不写太详细了,因为前面的章节基本上都有了,我直接就一口气都写出来需要那些文件然后在直接贴代码,首先 ...
- Mybatis学习笔记之二(动态mapper开发和spring-mybatis整合)
一.输入映射和输出映射 1.1 parameterType(输入类型) [传递简单类型] 详情参考Mybatis学习笔记之一(环境搭建和入门案例介绍) 使用#{}占位符,或者${}进行sql拼接. [ ...
- SpringBoot整合JavaWeb
一.SpringBoot整合Servlet的两种方式 1.通过注解扫描完成Servlet组件的注册 编写Servlet package com.example.demo.servlet; import ...
- spring-mybatis整合异常2
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...
- spring-mybatis整合异常
Failed to read artifact descriptor for XXXXXX:jar:XXXX.RELEAS 原因是maven的本地仓库没有设置好.在别处拷贝过来的项目会有自己的仓库位置 ...
- spring-mybatis整合项目 异常处理2
org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/imooc ...
- spring-mybatis整合项目 异常处理
java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorIm ...
- SSM-Spring-22:Spring+Mybatis+JavaWeb的整合
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 众所周知,框架Spring来整合别的框架,但是Mybatis出现的晚,Spring就没有给他提供支持,那怎么办 ...
- 【Spring】XML配置整合Mybatis
注意:项目开发使用了mybatis的mapper代理! 首先是mybatis自己的配置文件,被spring整合之后,只有typeAliases存在了,其他都整合在了spring-mybatis.xml ...
随机推荐
- git flow常用命令
https://danielkummer.github.io/git-flow-cheatsheet/index.zh_CN.html https://blog.csdn.net/shu580231/ ...
- Openresty 安装第三方插件
Openresty 安装第三方插件 程序媛没有夜生活 2016.08.02 15:33* 字数 167 阅读 1283评论 0喜欢 2 在安装之前,我们先来看一下我们现有的模块. 1.将需要安装的插件 ...
- Centos ssh 限制ip访问
要确定客户端计算机是否允许连接到服务,TCP包装器将引用以下两个文件,这两个文件通常称为主机访问文件: /etc/hosts.allow /etc/hosts.deny 当TCP包裹服务接收到客户端请 ...
- cocos2d-x C++ iOS工程集成第三方支付宝支付功能
一.在支付宝开放平台下载支付宝SDK(https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.WWgVz8&tr ...
- 多么痛的领悟---关于RMB数据类型导致的元转分分转元的bug
关于金额的数据类型,以及元转分分转元之间这种转换,以及元和分的比较,我相信很多人都踩过坑. 反正我是踩过. 而且,昨天和今天又重重的踩了两脚. 代付查询接口,支付中心给溢+响应的报文里,amount的 ...
- Sklearn的使用
初步接触要求时,从上图选自己数据所适用的方法, 首先看数据的样本是否 >50,小于则需要收集更多的数据 然后看问题适合分类.回归.聚类.降维中的哪一大类 Sklearn解决问题的一般步骤: 1. ...
- RN项目中使用react-native-elements报错: Unrecognized font family 'Material Icons'
查询了一些方案,但各自的环境不尽相同,最后在google中找到了答案.主要问题在于 (1)版本问题 (2)Xcode配置问题 报错如下 解决步骤: 1 . 首先需要正确安装 npm i -S reac ...
- LeetCode111.二叉树的最小深度
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- c#中枚举类型的定义与使用
介绍枚举是一个指定的常数,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.定义默认基数从O开 ...
- c#winform,知道图像路径,怎么在程序运行时往image里面添加图片
貌似可以直接添加啊 ,要改变显示的图片,就是将图片的路径赋值给picturebox即可pictureBox1.ImageLocation="图片路径"动态的改变这个值就行了.