spring-ssm整合(全配置xml或配置加注解)

全配置xml整合

在这里插入图片描述

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" id="WebApp_ID" version="2.5">
  <display-name>aspring-web-servlet</display-name>
  <welcome-file-list>
    <welcome-file>/jsp/index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置dispatcherServlet -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<!-- 配置servlet的初始化参数,读取springMVC的配置文件,创建spring容器 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring-mvc.xml</param-value>
  	</init-param>
  	<!-- 配置servlet启动时加载对象 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 配置spring的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置加载类路径的配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring-*.xml</param-value>
  </context-param>
  
  <!-- 配置过滤器,解决中文乱码的问题 -->
  <filter>
  	<filter-name>characterEncoding</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<!-- 指定字符集 -->
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>characterEncoding</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

spring-mybatis.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 			<!-- 设置MyBatis主配置文件的位置 -->
 			<property name="configLocation" value="classpath:mybatisconfig.xml" />
 			<!-- 设置数据源 -->
 			<property name="dataSource" ref="dataSource" />
 		</bean>	
	
 		<!-- 获取SqlSession
 			获取Dao接口实现类对象  sqlSession.getMapper()
 			MapperScannerConfigurer:自动扫描指定包(dao接口所在的包)
 			自动生成所有dao接口的实现类,对应bean的id:接口是StudentDao,接口的实现类id是:studentDao
 		 -->
 		 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 	<property name="basePackage" value="com.woniu.dao"/>
 		 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 		 </bean>

spring-db.xml

<!-- 配置数据源 -->
 		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 			<property name="driverClass" value="${jdbc.driver}" />
			<property name="jdbcUrl" value="${jdbc.url}" />
			<property name="user" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
 		</bean>
 		<!-- 指定四大参数properties文件的位置 -->
 		<context:property-placeholder location="classpath:jdbc.properties" />

mybatisconfig.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="com.woniu.entity"/>
	</typeAliases>
	<!-- jdbc参数由spring管理 -->

	<mappers>
		<package name="com.woniu.dao"/>
	</mappers>
</configuration>

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///infobase
jdbc.username=root
jdbc.password=123456

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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> 
 				
		<bean id="/my.do" class="com.woniu.controller.StudentController">
			<property name="studentService" ref="studentService" />
		</bean>
</beans>

spring-service.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> 
 				
		<bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
			<property name="studentDao" ref="studentDao" />
		</bean>
</beans>

StudentController.java

public class StudentController implements Controller {
	StudentService studentService;
	
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 从前端获取数据
		String sname = req.getParameter("sname");
		int sage = Integer.parseInt(req.getParameter("sage"));
		char ssex = req.getParameter("ssex").charAt(0);
		int cid = Integer.parseInt(req.getParameter("cid"));
		Student student = new Student(sname, sage, ssex, cid);
		// 执行添加学生方法
		studentService.addStudent(student);
		mv.addObject("msg", "插入成功");
		mv.setViewName("/jsp/index.jsp");
		return mv;
	}
}

StudentDao.java

public interface StudentDao {
	void insertStudent(Student student);
}

StudentDao.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.woniu.dao.StudentDao">
	<insert id="insertStudent">
		insert into student values(null,#{sname},#{sage},#{ssex},#{cid},0)
	</insert>
</mapper>

Student.java

public class Student {
	private Integer sid;
	private String sname;
	private Integer sage;
	private Character ssex;
	private Integer cid;
	
	public Student() {
		super();
	}
	
	public Student(String sname, Integer sage, Character ssex, Integer cid) {
		super();
		this.sname = sname;
		this.sage = sage;
		this.ssex = ssex;
		this.cid = cid;
	}

	public Student(Integer sid, String sname, Integer sage, Character ssex, Integer cid) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.sage = sage;
		this.ssex = ssex;
		this.cid = cid;
	}
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Integer getSage() {
		return sage;
	}
	public void setSage(Integer sage) {
		this.sage = sage;
	}
	public Character getSsex() {
		return ssex;
	}
	public void setSsex(Character ssex) {
		this.ssex = ssex;
	}
	public Integer getCid() {
		return cid;
	}
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + ", ssex=" + ssex + ", cid=" + cid + "]";
	}	
}

StudentService.java

public interface StudentService {
	void addStudent(Student student);
}

StudentServiceImp.java

public class StudentServiceImp implements StudentService{
	//配置文件注入,完成初始化
	StudentDao studentDao;
	
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void addStudent(Student student) {
		studentDao.insertStudent(student);
	}
}

dao配置+Service、Controller注解

除了以下类和xml文件不一样,其余的与上面全配置xml一样

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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> 
 				
		<!-- <bean id="/my.do" class="com.woniu.controller.StudentController">
			<property name="studentService" ref="studentService" />
		</bean> -->
		
		<context:component-scan base-package="com.woniu.controller"></context:component-scan>
</beans>

spring-service.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> 
 				
		<!-- <bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
			<property name="studentDao" ref="studentDAO" />
		</bean> -->
		<context:component-scan base-package="com.woniu.service"></context:component-scan>
</beans>

StudentController.java

@Controller
public class StudentController{
	//自动注入
	@Autowired
	StudentService studentService;
	
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	@RequestMapping("/my.do")
	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 从前端获取数据
		String sname = req.getParameter("sname");
		int sage = Integer.parseInt(req.getParameter("sage"));
		char ssex = req.getParameter("ssex").charAt(0);
		int cid = Integer.parseInt(req.getParameter("cid"));
		Student student = new Student(sname, sage, ssex, cid);
		// 执行添加学生方法
		studentService.addStudent(student);
		mv.addObject("msg", "插入成功");
		mv.setViewName("/jsp/index.jsp");
		return mv;
	}
}

StudentService.java

@Service
public class StudentServiceImp implements StudentService{
	//自动注入
	@Autowired
	StudentDAO studentDao;
	
	public void setStudentDao(StudentDAO studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void addStudent(Student student) {
		studentDao.insertStudent(student);
	}
}

v.setViewName("/jsp/index.jsp");
return mv;
}
}


StudentService.java

```java
@Service
public class StudentServiceImp implements StudentService{
	//自动注入
	@Autowired
	StudentDAO studentDao;
	
	public void setStudentDao(StudentDAO studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void addStudent(Student student) {
		studentDao.insertStudent(student);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值