SSM(SpringMVC Spring Mybatis)框架整合搭建
1、新建一个web工程。
2、首先看一下整体的框架结构:
3、将ssm框架搭建所需要的jar包复制到lib目录下
3、需要配置各个配置文件。
1)配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-encache.xsd"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>qzone</display-name>
<welcome-file-list>
<welcome-file>user.jsp</welcome-file>
</welcome-file-list>
<!-- spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 添加springmvc的支持 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2)在resource/spring下创建applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 指定要扫描的包com.etc下的所有 -->
<context:component-scan base-package="com.ekingwin.bas.cloud.user"></context:component-scan> <!-- 配置数据源
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean> -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:*.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
</bean> <!-- 配置service的事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.etc.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config> <!-- 配置mybatis的SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 自动扫描Mapper.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- mybaits配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
</bean> <!-- spring将mybatis下的sqlSessionFactory注入到daoceng -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ekingwin.bas.cloud"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice> </beans>
3)在resource/spring下创建spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 指定注解要扫描的包 -->
<context:component-scan base-package="com.ekingwin.bas.cloud.user.web"></context:component-scan>
<!-- 视图解析器,自动加上前缀和后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件 -->
<mvc:annotation-driven /> </beans>
4)在resource/mybatis下创建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>
<!-- 批量定义别名,使得com.etc.entity包下的所有bean都使用别名(别名就是类名,首字母大写或小写都可以) -->
<typeAliases>
<package name="com.ekingwin.bas.cloud"/>
</typeAliases>
</configuration>
5)在resource下创建dbconfig.properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
6)在resource下创建log4j.properties文件:
<span style="font-size:18px;">log4j.rootLogger=info,appender1,appender2
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logFile.txt
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout</span>
7)在resource/mapper下创建UserMapper.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="com.ekingwin.bas.cloud.user.dao.IUserDao">
<!-- <resultMap type="User" id="UserResult">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap> -->
<!-- <select id="login" parameterType="User" resultMap="UserResult">
select * from user where username=#{username}
</select> -->
<select id="getInfo" parameterType="String" resultType="map">
select * from user where username=#{username}
</select>
</mapper>
8)在user/web下的到下创建UserController.java文件:
package com.ekingwin.bas.cloud.user.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import com.ekingwin.bas.cloud.user.UserException;
import com.ekingwin.bas.cloud.user.dao.entity.User;
import com.ekingwin.bas.cloud.user.dto.UserDto;
import com.ekingwin.bas.cloud.user.service.IUserService;
import com.ekingwin.bas.cloud.user.service.impl.UserServiceImpl; //@RestController
@Controller
@RequestMapping("/user")
public class UserController { @Autowired
// @Qualifier(value = "userMetaServiceImpl")
private IUserService userService;
//
// @Resource
// private UserServiceImpl userService;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login (UserDto user) throws UserException{
System.out.println("123123");
String userresult = userService.getmessage(user);
return "redirect:/success.jsp";
}
// @RequestMapping(value = "/login", method = RequestMethod.POST)
// public String login (@RequestBody UserDto user) throws UserException{
// System.out.println("123123");
// String userresult = userService.getmessage(user);
// return "redirect:/success.jsp";
// }
}
9)在user/service下创建IUserService.java:
package com.ekingwin.bas.cloud.user.service; import javax.servlet.http.HttpServletRequest; import com.ekingwin.bas.cloud.user.UserException;
import com.ekingwin.bas.cloud.user.dto.UserDto; public interface IUserService { public String getmessage(UserDto user) throws UserException; }
10)在user/service/impl下创建UserServiceImpl.java文件:
package com.ekingwin.bas.cloud.user.service.impl; import java.util.List;
import java.util.Map; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service; import com.ekingwin.bas.cloud.user.UserException; import com.ekingwin.bas.cloud.user.dao.IUserDao;
import com.ekingwin.bas.cloud.user.dao.entity.User;
import com.ekingwin.bas.cloud.user.dto.UserDto;
import com.ekingwin.bas.cloud.user.service.IUserService; @Primary
@Service
public class UserServiceImpl implements IUserService{ // @Resource
// private UserDao userDao; @Autowired
private IUserDao userDao; public User login(User user) {
User users = userDao.login(user);
System.out.println("12313247092137498123");
return users;
} @Override
public String getmessage(UserDto user) throws UserException {
// TODO Auto-generated method stub String username = user.getUsername();
List<Map<String, Object>> list = userDao.getInfo(username);
System.out.println(list);
System.out.println("123123");
return null;
}
} //@Service
//public class UserServiceImpl implements IUserService{
//
//
// @Autowired
// private UserDao userDao;
//
//
// @Override
// public String getmessage(UserDto user) throws UserException {
//// String username = user.getUsername();
//// String username=user.getParameter("username");
//// List<Map<String,Object>> list =userDao.getInfo(username);
//// System.out.println(list.get(0).get("username"));
// return null;
// }
// }
11)user/dto下创建UserDto.java文件:
package com.ekingwin.bas.cloud.user.dto; public class UserDto {
private String id;
private String username;
private String password;
private String company;
private String age;
private String sex; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}
12)user/dao下创建IUserDao.java文件:
package com.ekingwin.bas.cloud.user.dao; import java.util.List;
import java.util.Map; import org.apache.ibatis.annotations.Param; import com.ekingwin.bas.cloud.user.dao.entity.User;
import com.ekingwin.bas.cloud.utils.BasMapper; public interface IUserDao extends BasMapper<User>{
public User login(User user);
public List<Map<String, Object>> getInfo(@Param("username")String username);
}
13)在user/dao/entity下创建User.java文件:
package com.ekingwin.bas.cloud.user.dao.entity; import javax.persistence.Id;
import javax.persistence.Table; //@Table(name = "user")
public class User { @Id
private String id;
private String username;
private String password;
private String company;
private String age;
private String sex; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}
14)在WebContent下创建user.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<!-- <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="./js/jquery-3.1.0.min.js"></script>
</head>
<body>
<form action="user/login.do" method="post">
username:<input type="text" name="username" value="${user.username}"/>
<input type="submit" value="login"/><font color="red">${errorMsg}</font>
</form>
</body>
<!-- <body>
<div>
<button id="subBtn">测试</button>
</div>
<script type= "text/javascript">
$().ready(function(){
document.getElementById("subBtn").onclick = function () {
request3();
};
//=========================请求方式3================//
var url = "/SSM/user/login.do";
var data = {"username":"小名" };
var request3 = function (){
$.ajax({
contentType: 'application/json;charset=UTF-8',
url: url,
type: 'POST',
data: JSON.stringify(data),
dataType: 'json'
})
.done(function (res) {
console.log(res);}
)
.fail(function (jqXHR, textStatus, errorThrown) {
// console.log("请求发送错误");
});
};
});
</script>
</body>-->
</html>
15)在WebContent下创建success.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
欢迎登录
</body>
</html>
4、启动Tomcat在浏览器上即可。
成功后跳转:
SSM(SpringMVC Spring Mybatis)框架整合搭建的更多相关文章
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- SpringMVC Spring MyBatis 框架整合 Annotation MavenProject
项目结构目录 pom.xml jar包管理 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- SSM(SpringMVC+Spring+Mybatis)框架程序on IDEA
有了之前文章搭建的SSH框架之后,现在搭建基于Mybatis的框架.主要基于如下这篇文章: http://blog.csdn.net/gallenzhang/article/details/51932 ...
- SSM(SpringMVC+Spring+MyBatis)三大框架使用Maven快速搭建整合(实现数据库数据到页面进行展示)
本文介绍使用SpringMVC+Spring+MyBatis三大框架使用Maven快速搭建一个demo,实现数据从数据库中查询返回到页面进行展示的过程. 技术选型:SpringMVC+Spring+M ...
- 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...
- springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
@_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...
- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...
- JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合
搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例 a)准备 ...
- struts2 + spring + mybatis 框架整合详细介绍
struts2 + spring + mybatis 框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...
随机推荐
- mysql自动断开该连接解决方案
mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 作者: MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,MySQL 就会断开该连接,而 c3p0 连接池则以为该被断 ...
- java 反射的基本操作
一.反射的概述JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为j ...
- ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)【奇葩的bug】
# Bug描述 今天周末,在家里学点新技术,虽然公司分配的任务没有完成(滑稽滑稽) 我先创建了一个mysql数据库,用root用户创建一个新用户,毕竟项目中使用root是非常危险的,尤其是我这样的实 ...
- mpvue——支持less
安装 安装less和less-loader,我用的是淘宝源,你也可以直接npm $ cnpm install less less-loader --save 配置 打开build目录下的webpack ...
- 利用 Python_tkinter 完成 2048 游戏
成品展示 具备基本的数据合并以及分数统计,不同数字的色块不同 产生随机数, 数据无法合并判定以及重新开始选项 同时可以判定游戏失败条件 需求分析 完成基本数据合并算法 游戏结束条件 界面展示 重置按钮 ...
- jmeter5.1测试websocket接口
jmeter没有websocket协议的取样器,需要我们自己开发,但是网上已经有大神先开发好了,[相关jar包,点击左侧加群获取] 只需要放到jmeter的ext目录(D:\apache-jmeter ...
- Wannafly挑战赛23 T2游戏 SG函数
哎,被卡科技了,想了三个小时,最后还是大佬给我说是\(SG\)函数. \(SG\)函数,用起来很简单,证明呢?(不可能的,这辈子都是不可能的) \(SG\)定理 游戏的\(SG\)函数就是各个子游戏的 ...
- Fiddler--Filters
本篇主要介绍Fiddler中Filters(过滤器)选项功能. 先看看Filters的界面: 一.模块一 当勾选“Use Filters”,Filters才开始工作:否则Filters中的设置内容将无 ...
- SNMP学习——v3 VACM
目录: ☆ SNMPv3视图访问控制模型 ☆ SNMPv3报文格式 ☆ VACM参数 ☆ Context Table ☆ Security To Group Table ...
- Sass学习第一天
Sass学习 网站学习地址: Sass中文网:https://www.sass.hk/docs/#t7-3 Airen的博客:https://www.w3cplus.com/preprocessor/ ...