spring、springmvc、mybatis整合搭建ssm项目(Eclipse+maven)
2017-10-11 21:22
841 查看
1、创建maven项目
2、添加相关依赖包
配置pom.xml:<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.danni</groupId> <artifactId>ssm-maven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 固定spring版本号 --> <properties> <springframe.version>4.3.9.RELEASE</springframe.version> </properties> <dependencies> <!-- Spring 依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${springframe.version}</version> </dependency> <!-- Spring 依赖 --> <!-- DB 相关依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- DB 相关依赖 --> <!-- Jsp,Servlet 相关依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <!-- 提供范围 使用容器提供的jar --> <scope>provided</scope> </dependency> <!-- Jsp,Servlet 相关依赖 --> </dependencies> </project>
3、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_2_5.xsd" version="2.5"> <display-name>ssm-maven</display-name> <!-- application参数配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 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:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
4、配置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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 组件自动扫描,对于添加了注解标签的 --> <context:component-scan base-package="com.danni.model"></context:component-scan> <!-- 引入jdbc配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <!-- 数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- 配置 SessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> 10c94 <!-- DAO接口所在包名,Spring会自动查找其下的类(修改这里的value值:你的dao所在包名) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.danni.model.dao"></property> </bean> <!-- (事务管理) --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
jdbc.properties配置文件:
driver = com.mysql.jdbc.Driver url = jdbc:mysql:///student?characterEncoding=utf-8 username = root password = root
5、配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 注解驱动 如果开启了静态资源访问必须开启当前配置 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 当前com.jdcy.web.controller包中所有类,只要类上打上了@Controller注解,容器将自动将该类装配进去 --> <context:component-scan base-package="com.danni.web.controller"/> <!-- 配置访问前后缀(这样在写比如同一个文件夹views下的views/xxx.jsp页面就可以写成xxx了,很方便) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 开启静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"></mvc:resources> <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> <mvc:resources location="/js/" mapping="/js/**"></mvc:resources> </beans>
6、通过mybatis插件自动生成dao接口及映射文件,entity实体类
参考这篇文章的方法:http://blog.csdn.net/qq_36748278/article/details/782088307、以登录为例让你理解ssm
我的项目结构如下:(1)添加AdminsService.java接口
@Service //注解表明这是个Service public interface AdminsService{ public Admins login(Admins admins); }
(2)添加AdminsServiceImpl.java实现类
@Service //注解表明这是个Service @Transactional //注解事务 public class AdminsServiceImpl implements AdminsService{ @Autowired //这样就不用再设置get和set方法了 private AdminsMapper AdminsMapper; @Override public Admins login(Admins admins) { return AdminsMapper.login(admins); } }
(3)添加对应的AdminsController.java
@Controller //注解表明这是个Controller @RequestMapping(value = "admin") //处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。 public class AdminsController { @Autowired private AdminsService adminsService; //在浏览器中可以通过admin/login来访问 @RequestMapping(value = "login", method = RequestMethod.GET) public String toLogin() { return "login"; } @RequestMapping(value = "login", method = RequestMethod.POST) public String dologin(@ModelAttribute Admins admins, HttpSession session) { //@ModelAttribute :运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用 Admins admins2 = adminsService.login(admins); if(admins2 == null){ return "login"; } return "redirect:/emp/query"; } }
(4)添加对应login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html> <html lang="zh-CN"> <head> <base href="<%=basePath%>"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Login</title> <!-- Bootstrap --> <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="css/my.css" /> </head> <body> <div class="container"> <form class="form-signin" action="admin/login" method="post"> <label for="account" class="sr-only">Account</label> <!--注意这里的name值需要和你的entity类中的成员变量对应相同,不然获得不到的--> <input type="text" id="account" name="adminname" class="form-control" placeholder="账 号" required autofocus> <label for="password" class="sr-only">Password</label> <input type="password" id="password" name="adminpwd" class="form-control" placeholder="密 码" required> <button class="btn btn-lg btn-primary btn-block" type="submit">登录</button> </form> </div> <script src="js/jquery.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html>
(5)在浏览器访问http://localhost:8080/ssm-maven/admin/login即可
(6)当需要传递参数需要这样操作:
//根据id查看 @RequestMapping(value="query/{id}",method=RequestMethod.GET) //value后面跟你的参数,如果有多个参数可以这样value="query/{id}/{name}" public ModelAndView queryByid(@PathVariable Integer id){ //参数需要用@PathVariable + 类型 + 参数名的形式 Employee employee = employeeService.queryById(id); ModelAndView mv = new ModelAndView(); //视图解释器解析 mv.addObject("employee", employee); mv.setViewName("detail"); return mv; }
相关文章推荐
- Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建+SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- SSM整合,spring4.3.6.RELEASE,springmvc,mybatis3.4.2,maven聚合项目整合
- Eclipse Maven建web项目 与SSM三大框架整合教程(Spring+SpringMVC+MyBatis)
- Eclipse下使用Maven整合SSM(spring springmvc mybatis)
- IntelliJ IDEA 搭建基于Maven 的SSM(一)(spring,springMvc,Mybatis)框架整合
- eclipse构建maven项目整合SSM(spring+springMVC+mybatis)框架
- Eclipse Maven建web项目 与SSM三大框架整合教程(Spring+SpringMVC+MyBatis)
- 传统web方式整合SSM(spring、springMVC、mybatis)搭建SSM项目
- SSM基于注解配置Spring4.1.6、springMVC4.1.6、mybatis3.2.8的maven框架搭建
- 从零开始搭建maven+springmvc+mybatis web项目(2)---springmvc配置文件编写
- SSM(Spring+SpringMVC+Mybatis)+Maven项目搭建
- 使用maven搭建SSM项目(spring+springmvc+Mybatis)
- 基于maven搭建ssm开发框架(1) mybatis和spring的整合
- myeclipse下使用maven搭建SSM(spring、springmvc、mybatis)框架
- java搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目
- SSM框架搭建详细过程,springmvc、spring、mybatis整合
- Spring+SpringMVC+Mybatis整合系列(二)Eclipse新建Maven web项目
- IntelliJ IDEA下SSM框架整合、创建Maven项目流程(Spring + SpringMVC + MyBatis)
- spring-springmvc-mybatis(ssm)maven框架整合教程
- 三、maven项目搭建 springmvc+ spring + mybatis(springmvc搭建)