SSM整合

jar包:

目录结构:

web.xml主要内容: 

<servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:conf/dispatcherServlet.xml</param-value>
  	</init-param>
  	<!-- 当服务器启动的时候,创建对象 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 注册字符集过滤器,解决乱码问题 -->
  <filter>
  	<filter-name>characterEncodingFilter</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>forceRequestEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>	
  </filter-mapping>

dispatcherServlet对应的dispatcherServlet.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">
	
	<!-- 声明组件扫描器,指定Controller注解所在的包名 -->
	<context:component-scan base-package="com.test.controllers" />
	
	<!-- 声明视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 声明注解驱动 -->
	<mvc:annotation-driven />
	
	<!-- 处理静态资源 -->
	<mvc:resources location="/images/" mapping="/images/**" />
	<mvc:resources location="/js/" mapping="/js/**" />
	
	<!-- 声明组件扫描器,指定@Service所在的包名 -->
	<context:component-scan base-package="com.test.service" />
	
	<!-- 加载属性配置文件 -->
	<context:property-placeholder location="classpath:conf/jdbc.properties"/>
	
	<!-- Druid数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- 声明SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation"  value="classpath:conf/mybatis.xml"  />
	</bean>

	<!-- 声明mybatis的动态代理,创建dao对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    	<property name="basePackage" value="com.test.dao" />
	</bean>
</beans>

数据库配置文件:jdbc.properties:

jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

mybatis.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.test.beans"/>
	</typeAliases>
	
	<!-- 配置sql映射文件 -->
	<mappers>
		<package name="com.test.dao"/>
	</mappers>
</configuration>

mybatis的动态代理要求dao包下,sql映射文件和dao接口文件名称一致

 StudentDao.java:

package com.test.dao;

import java.util.List;

import com.test.beans.Student;

public interface StudentDao {

	int insertStudent(Student student);
	List<Student> selectStudents();
}

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.test.dao.StudentDao">

	<insert id="insertStudent">
		insert into student(name,age) values(#{name},#{age})
	</insert>
	
	<select id="selectStudents" resultType="Student">
		select id, name,age from student order by id desc
	</select>

</mapper>

Student bean对象在此忽略,无非id,name,age三个属性

service目录:

 只看实现类StudentServiceImpl.java:

package com.test.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.beans.Student;
import com.test.dao.StudentDao;
import com.test.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

	/**
	 * 定义引用类型   @Autowired , @Resource ; byName, byType
	 */
	//byType
	@Autowired
	StudentDao studentDao;
	
	@Override
	public int addStudent(Student student) {
		int rows  = studentDao.insertStudent(student);
		return rows;
	}

	@Override
	public List<Student> queryStudents() {

		List<Student> students = studentDao.selectStudents();
		return students;
	}

}

controllers下看控制器StudentController.java:

package com.test.controllers;

import java.util.ArrayList;
import java.util.List;

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

import com.test.beans.Student;
import com.test.service.StudentService;

@Controller
public class StudentController {
	
	@Autowired
	StudentService studentService;

	@RequestMapping(value = "addStudent")
	public ModelAndView studentList(Student student)
	{
		ModelAndView mv = new ModelAndView();
	
		//调用Service,执行业务处理
		int rows = studentService.addStudent(student);
		if( rows > 0 ){
			mv.addObject("msg", "注册成功!!!");
			//视图的逻辑名称
			mv.setViewName("success");
		} else {
			mv.addObject("msg", "注册失败!!!");
			//视图的逻辑名称
			mv.setViewName("fail");
		}
		return mv;
	}
	
	@RequestMapping(value="queryStudent")
	@ResponseBody
	public List<Student> queryStudent(){
		List<Student> students  = studentService.queryStudents();
		//对结果做处理
		if( students == null){
			students = new ArrayList<Student>(); 
		}
		return students; // 不会为null--json ([{},{}...]; [] ))
	}
}

静态资源忽略。至此SSM框架基本配置完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值