欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java开发 spring整合springMVC(+mybatis)

程序员文章站 2022-06-28 16:46:53
spring整合springMVC一、步骤1、添加springMVC所需jar包1、项目包结构2、在web.xml文件里配置请求分发器springMVC(DispatcherServlet)

spring整合springMVC

一、步骤

1、添加springMVC所需jar包

java开发 spring整合springMVC(+mybatis)

1、项目包结构

java开发 spring整合springMVC(+mybatis)

2、在web.xml文件里配置请求分发器springMVC(DispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>SpringAndMybatis</display-name>
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 默认的springMVC配置文件名称为[servlet-name]-servlet.xml,并且默认位置在/WEB-INF/[servlet-name]-servlet.xml 
			这里更改默认配置,使用自己定义的springMVC配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMVC-config.xml</param-value>
		</init-param>
		<!-- 程序启动立即启动 -->
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

3、编写springMVC配置文件

springMVC-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
		<!-- 
		基于注解的springMVC使用配置
		<mvc:annotation-driven/> 覆盖了DispatcherServlet对象的很多默认配置信息
		相当于注册了一个RequestMappingHandlerMapping,一个RequestMappingHandlerAdapter
		给@RequestMapping等注解提供了支持,并提供很多其他类似数据绑定等的功能。
	 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 加入组件搜索路径 comonent-scan,确保可以识别@Controller注解。 -->
	<context:component-scan base-package="controller"></context:component-scan>
</beans>

4、在web.xml里配置spring容器随项目启动而启动

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>SpringAndMybatis</display-name>
	<!-- 
		以下三个<context-param>容器初始化参数是用来解决下面抛出的异常
		threw NamingException with message: Name [spring.profiles.active] is not bound in this Context
		threw NamingException with message: Name [spring.profiles.default] is not bound in this Context
		threw NamingException with message: Name [spring.liveBeansView.mbeanDomain] is not bound in this Context 
		虽然不影响程序的正常运行,但解决了总是看得舒服点
	-->
	<context-param>
		<param-name>spring.profiles.active</param-name>
		<param-value>dev</param-value>
	</context-param>
	<context-param>
		<param-name>spring.profiles.default</param-name>
		<param-value>dev</param-value>
	</context-param>
	<context-param>
		<param-name>spring.liveBeansView.mbeanDomain</param-name>
		<param-value>dev</param-value>
	</context-param>
	<!-- 配置spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-config.xml</param-value>
	</context-param>
	<!-- 加载spring的配置文件 -->
  <!-- 这是一个监听器,当项目启动的时候会加载web的初始化参数值,然后创建spring的容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 配置请求分发器springMVC -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 默认的springMVC配置文件名称为[servlet-name]-servlet.xml,并且默认位置在/WEB-INF/[servlet-name]-servlet.xml 
			这里更改默认配置,使用自己定义的springMVC配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMVC-config.xml</param-value>
		</init-param>
		<!-- 程序启动立即启动springMVC配置 -->
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>
这算是一个完整版的web.xml文件,整合了spring和springMVC

2、全部代码

Student.java

package pojo;

public class Student {
	private int sno;
	private String sname;
	private int sage;

	public Student() {
	}

	public Student(int sno, String sname, int sage) {
		this.sno = sno;
		this.sname = sname;
		this.sage = sage;
	}

	public int getSno() {
		return sno;
	}

	public void setSno(int sno) {
		this.sno = sno;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public int getSage() {
		return sage;
	}

	public void setSage(int sage) {
		this.sage = sage;
	}

	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", sage=" + sage + "]";
	}

}

StudentService.java

package service;

import java.util.List;

import pojo.Student;

public interface StudentService {
	public List<Student> setectAllStudent();
	public void addStudent(Student student);
	public void updateStudent(Student student);
	public void deleteStudent(int id);
}

StudentServiceImpl.java

package service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.StudentMapper;
import pojo.Student;
import service.StudentService;

@Service
public class StudentServiceImpl implements StudentService{
	
	@Autowired
	SqlSessionFactory sessionFactory;

	public List<Student> setectAllStudent() {
		SqlSession sqlSession = sessionFactory.openSession();
		StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
		List<Student> students = mapper.setectAllStudent();
		students.forEach(System.out::println);
		return students;
	}

	public void addStudent(Student student) {
		
	}

	public void updateStudent(Student student) {
		
	}

	public void deleteStudent(int id) {
		
	}

}

StudentMapper.java

package dao;

import java.util.List;

import pojo.Student;

public interface StudentMapper {
	public List<Student> setectAllStudent();
	public void addStudent(Student student);
	public void updateStudent(Student student);
	public void deleteStudent(int id);
}	

StudentMapper.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="dao.StudentMapper">
	<select id="setectAllStudent" resultType="pojo.Student">
		SELECT "sno","sname","sage" FROM "student"
	</select>
</mapper>

StudentControler.java

package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import service.StudentService;

@Controller
public class StudentControler {
	@Autowired
	StudentService service;
	
	@RequestMapping("/selectStudent")
	public void selectStudent(String name,String age) {
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		service.setectAllStudent();
	}
}

db.properties

url=jdbc:oracle:thin:@localhost:1521:xe
driver=oracle.jdbc.OracleDriver
user=myuser
password=myuser

log4j.properties

log4j.rootLogger=DEBUG, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] %c - %m%n

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>
	<mappers>
		<mapper class="dao.StudentMapper" />
	</mappers>
</configuration> 

spring-config.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="http://www.springframework.org/schema/context"
		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">
	<context:component-scan base-package="service"></context:component-scan>
	<!-- 配置sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 
		有mybatis的xml配置文件时这样配置
		<property name="configLocation" value="classpath:mybatis-config.xml"></property> 
		-->
		<!-- 无mybatis的xml配置文件时这样配置 -->
		<property name="mapperLocations">
			<array>
				<value>classpath:dao/StudentMapper.xml</value>
			</array>
		</property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
		<property name="username" value="myuser"></property>
		<property name="password" value="myuser"></property>
	</bean>
	<!-- 配置扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		<property name="basePackage" value="dao"></property>
	</bean>
	
</beans>

springMVC-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
		<!-- 
		基于注解的springMVC使用配置
		<mvc:annotation-driven/> 覆盖了DispatcherServlet对象的很多默认配置信息
		相当于注册了一个RequestMappingHandlerMapping,一个RequestMappingHandlerAdapter
		给@RequestMapping等注解提供了支持,并提供很多其他类似数据绑定等的功能。
	 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 加入组件搜索路径 comonent-scan,确保可以识别@Controller注解。 -->
	<context:component-scan base-package="controller"></context:component-scan>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>SpringAndMybatis</display-name>
	<!-- 
		以下三个<context-param>容器初始化参数是用来解决下面抛出的异常
		threw NamingException with message: Name [spring.profiles.active] is not bound in this Context
		threw NamingException with message: Name [spring.profiles.default] is not bound in this Context
		threw NamingException with message: Name [spring.liveBeansView.mbeanDomain] is not bound in this Context 
		虽然不影响程序的正常运行,但解决了总是看得舒服点
	-->
	<context-param>
		<param-name>spring.profiles.active</param-name>
		<param-value>dev</param-value>
	</context-param>
	<context-param>
		<param-name>spring.profiles.default</param-name>
		<param-value>dev</param-value>
	</context-param>
	<context-param>
		<param-name>spring.liveBeansView.mbeanDomain</param-name>
		<param-value>dev</param-value>
	</context-param>
	<!-- 配置spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-config.xml</param-value>
	</context-param>
	<!-- 加载spring的配置文件 -->
  <!-- 这是一个监听器,当项目启动的时候会加载web的初始化参数值,然后创建spring的容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 配置请求分发器springMVC -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 默认的springMVC配置文件名称为[servlet-name]-servlet.xml,并且默认位置在/WEB-INF/[servlet-name]-servlet.xml 
			这里更改默认配置,使用自己定义的springMVC配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMVC-config.xml</param-value>
		</init-param>
		<!-- 程序启动立即启动springMVC配置 -->
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

本文地址:https://blog.csdn.net/qq_42485441/article/details/109643733

相关标签: spring mybatis