0028ssm环境搭建(springmvc+spring+mybatis)

spring整合springmvc和mybatis主要分为如下几个步骤:

1、spring环境搭建

2、springmvc环境搭建

3、spring整合springmvc

4、spring整合mybatis

具体如下:

先把最基本的会用到的内容创建出来

a、创建maven项目的过程(可参考之前创建springmvc项目的过程)

b、pom.xml中引入spring、springmvc、mybatis、jdbc、mysql、jstl等一系列相关jar包

c、创建controller类、service接口和实现类、dao接口、domain实体、数据库表(如果只是搭建spring环境,只需service即可,为了后面方便,此处统一建了)

1、spring环境搭建

spring环境搭建好的一个标志就是将service交给容器管理

a、applicationContext.xml中开启注解扫描,并排除扫描controller,因为controller是要交给springmvc管理的

b、service上加注解,交给容器管理,并可以给servcie起名字

c、编写测试类EmpTest中的run1()方法测试spring环境是否搭建好

2、springmvc环境搭建

springmvc环境搭建好的一个标志就是页面发送请求能够正常访问到controller中的方法

a、web.xml中配置前端控制器和中文乱码的过滤器

b、resources下创建springmvc.xml,并改约束xmlns

  1、开启注解扫描,只扫描controller

  2、配置视图解析器

  3、过滤静态资源-->webapp下建立相应目录

  4、开启springmvc注解的支持

c、创建index.jsp-->写标签访问controller的方法

d、controller上加@Controller注解交给容器管理,并写对应的查询方法,返回"list"字符串

e、在/WEB-INF/pages下建立list.jsp

f、部署项目并启动,进行简单测试

3、spring整合springmvc

整合成功的一个标志是controller能够调用到service中的方法

a、web.xml中配置监听器ContextLoaderListener和上下文初始话参数context-param,

  用监听器监听容器启动的时候加载上下文参数中配置的spring配置文件

b、controller中注入service

c、重新启动项目进行测试

4、spring整合mybatis

spring整合mybatis成功的标志是service能够调用到dao对象,且dao能操作数据库

a、在spring的配置文件中进行配置

  1、配置连接池

  2、配置SqlSessionFactory工厂(其中还需要配置映射文件路径mapperLocations

  3、配置扫描接口所在的包

b、dao上加上注解@MapperScan(写@Repository是不行的,无法与映射文件做关联,查询时会报错

c、service中注入dao,并调用dao的方法

 

目录结构如下:

0028ssm环境搭建(springmvc+spring+mybatis)_第1张图片

 

 

 0028ssm环境搭建(springmvc+spring+mybatis)_第2张图片

 

 

 

代码实现如下:

1、index.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/9
Time: 12:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


Title


<%--访问路径是相对路径,前边没有/--%>
springmvc测试

员工姓名:

邮箱:

性别

部门编号:




2、EmpController.java

package com.example.controller;

import com.example.domain.Employee;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
//service是spring管理的,只有spring整合了springmvc后才能依赖注入
@Autowired
private EmpService empService;
@RequestMapping("/queryusers")
public String queryusers(Model model){
List users = empService.queryusers();
System.out.println("执行EmpController中的queryusers");
model.addAttribute("users",users);
System.out.println(users);
return "list";
}
@RequestMapping("addEmp")
public void addEmp(Employee employee, HttpServletRequest request, HttpServletResponse response)throws Exception{
empService.addEmp(employee);
response.sendRedirect(request.getContextPath()+"/emp/queryusers");

}
}

3、EmpDao.java

package com.example.dao;

import com.example.domain.Employee;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Repository;

import java.util.List;

@MapperScan
/*@Repository*/
public interface EmpDao {
public List queryusers();

public void addEmp(Employee employee);
}

4、Employee.java

package com.example.domain;

public class Employee {
private int id;
private String lastName;
private String email;
private String gender;
private int dId;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public int getdId() {
return dId;
}

public void setdId(int dId) {
this.dId = dId;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender='" + gender + '\'' +
", dId=" + dId +
'}';
}
}

5、EmpMapper.xml









insert into employee values(null,#{lastName},#{email},#{gender},#{dId})

6、EmpServiceImpl.java

package com.example.service.impl;

import com.example.dao.EmpDao;
import com.example.domain.Employee;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service("empService")
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao empDao;
@Override
public List queryusers() {
List users = empDao.queryusers();
System.out.println("执行EmpServiceImpl中的queryusers方法");
return users;
}

@Override
// @Transactional
public void addEmp(Employee employee) throws RuntimeException{
System.out.println("执行EmpServiceImpl中的addEmp方法");
empDao.addEmp(employee);
int a=10/0;
System.out.println(a);
}
}

7、EmpTest.java

package com.example.test;

import com.example.service.EmpService;
import com.example.service.impl.EmpServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmpTest {
/*spring环境搭建测试 */
@Test
public void run1(){
//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
EmpService empService = (EmpServiceImpl)ac.getBean("empService");
//调用方法
empService.queryusers();

}
}

8、list.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/9
Time: 12:45
To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


Title


成功进入list页面



${user.lastName}


9、pom.xml



xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

com.example
springmvc-sping-mybatis
1.0-SNAPSHOT
war

springmvc-sping-mybatis Maven Webapp

http://www.example.com


UTF-8
1.7
1.7
5.0.2.RELEASE






org.springframework
spring-aop
${spring.version}




org.springframework
spring-aspects
${spring.version}



org.springframework
spring-beans
${spring.version}



org.springframework
spring-context
${spring.version}









org.springframework
spring-jdbc
${spring.version}



org.springframework
spring-test
${spring.version}



org.springframework
spring-tx
${spring.version}



org.springframework
spring-web
${spring.version}



org.springframework
spring-webmvc
${spring.version}


junit
junit
4.12
compile




org.mybatis
mybatis-spring
1.3.0



mysql
mysql-connector-java
8.0.13



org.mybatis
mybatis
3.4.1



c3p0
c3p0
0.9.1.2



jstl
jstl
1.2


javax.servlet
jsp-api
2.0
provided








src/main/java

**/*.properties
**/*.xml

false


springmvc-sping-mybatis



maven-clean-plugin
3.1.0



maven-resources-plugin
3.0.2


maven-compiler-plugin
3.8.0


maven-surefire-plugin
2.22.1


maven-war-plugin
3.2.2


maven-install-plugin
2.5.2


maven-deploy-plugin
2.8.2




10、web.xml

        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >


Archetype Created Web Application


org.springframework.web.context.ContextLoaderListener



contextConfigLocation
classpath:applicationContext.xml



dispatcherServlet
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:springmvc.xml


1



dispatcherServlet
/




characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



characterEncodingFilter
/*


11、applicationContext.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">












































 
 

12、springmvc.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">




















 

如有理解不到之处,望指正!

 

 

 

你可能感兴趣的:(0028ssm环境搭建(springmvc+spring+mybatis))