JavaEE之SSM框架整合

JavaEE之SSM框架整合

  • 创建四个包
  • 资源目录结构
  • 导入需要的Jar包
  • 编写配置文件(5个)
    • applicationContext.xml
    • springmvc-config.xml
    • mybatis-config.xml
    • web.xml
    • log4j.properties
  • 数据库建表并插入数据
  • 根据数据库的字段编写实体类
  • 编写Dao层
  • 编写Service层
  • 编写Controller层
  • jsp页面编写

PS:以下内容根据老师讲课内容进行的梳理

创建四个包

在src下创建四个包:entity、dao、service、controller

资源目录结构

在WebContent下建立文件夹:js, jsp, css, images 等

导入需要的Jar包

spring、aop、mybatis、mybatis-spring、dbcp、mysql驱动、lang、jackson等。以下是所有的包
JavaEE之SSM框架整合_第1张图片

编写配置文件(5个)

在src下创建以下5个文件

applicationContext.xml

  1. 管理service层的JavaBean 用扫描的方式
<context:component-scan base-package="com.service"></context:component-scan>
  1. 管理数据源 配置dbcp
    class的路径通过Web App Libraries找到dbcp2包的BasicDataSource,然后Copy Qualified Name
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
   		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/数据库名"></property>    
        <property name="username" value="用户名"></property>
        <property name="password" value="密码"></property>     
         <!-- 连接池配置 如最大连接数等  需要的配 这里没配-->
   </bean>

另:给数据库设置密码
① 点击数据库的用户
② 将这两个点进去 修改密码
在这里插入图片描述
③ 点击连接属性 将刚才的新密码填进去
即可完成。

  1. 配置数据库的事务处理
    ① 通知(transactionManager):注入属性; ref的内容是前面dbcp配置的id名
<bean id="transactinManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
</bean>
② 开启事务注解
  <tx:annotation-driven transaction-manager="transactinManager"></tx:annotation-driven>
③ 管理Mybatis: 数据源、configLocation
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        
</bean>
  1. mybatis-spring整合:扫描Dao层
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>       
</bean>

完整的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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
   <!-- 管理service层的JavaBean -->
   <context:component-scan base-package="com.service"></context:component-scan>
     
   <!-- 管理数据源   配置数据源 dbcp -->   
   <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
   		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/databaseName"></property>    
        <property name="username" value="username"></property>
        <property name="password" value="password"></property>     
         <!-- 连接池配置 如最大连接数等-->
   </bean>
   
   <!-- 数据库的事务管理 -->
   <bean id="transactinManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
    </bean>
     <!-- 开启事务注解 -->
     <tx:annotation-driven transaction-manager="transactinManager"></tx:annotation-driven>
     
     <!-- 管理mybatis -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        
     </bean>
   
   <!-- Mabatis-spring整合 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>       
   </bean>
</beans>

springmvc-config.xml

  1. 扫描控制层
  <context:component-scan base-package="com.controller"></context:component-scan>  
  1. 配置视图解析器
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/"></property>
       <property name="suffix" value=".jsp"></property>        
   </bean>
  1. mvc注解驱动
   <mvc:annotation-driven />
  1. 静态资源放行:如果不写该部分,就需要前端控制器处理,那么路径就可能会找不到。
   <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
   <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
   <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> 

完整的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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!-- 控制层的扫描 -->  
  <context:component-scan base-package="com.controller"></context:component-scan>  
   
   <!--  配置视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/"></property>
       <property name="suffix" value=".jsp"></property>        
   </bean>
   
   <!-- mvc注解驱动 -->
   <mvc:annotation-driven />
   <!-- 静态资源放行操作 -->
   <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
   <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
   <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>    
</beans>

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>
   <typeAliases>
       <package name="com.entity"/>       
   </typeAliases> 
</configuration>

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssm</display-name>
  <!-- 前端控制器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-config.xml</param-value>         
      </init-param>
      
      <load-on-startup>
        1
      </load-on-startup>
   </servlet>
   
   <servlet-mapping>
      <servlet-name>springMvc</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
   
  <!-- spring容器加载 -->
  
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 监听器 -->
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

log4j.properties

log4j.rootCategory=DEBUG, CONSOLE, LOGFILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{yyyy-MM-dd HH:mm:ss}  %m %n


log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=log.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%C %d{yyyy-MM-dd HH:mm:ss}  %m %n

数据库建表并插入数据

根据数据库的字段编写实体类

编写Dao层

下面两者的命名要一样
1、ProductMapper接口: 方法

public interface ProductMapper {
	public ProductEntity selectProduct(int id);
}

2、ProductMapper.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.dao.ProductMapper">
	<select id="selectProduct" resultType="productEntity">
	  select * from tb_product where id=#{id}
	</select>
</mapper>

编写Service层

1、service接口方法

public interface ProductService {
	//验证产品
	public boolean validProduct(int id);
}

2、实现service接口中定义的方法

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

import com.cqust.dao.ProductMapper;
import com.cqust.entity.ProductEntity;

@Service
public class ProductServiceImpl implements ProductService{

	@Autowired
	private ProductMapper productMapper;
	
	@Override
	public boolean validProduct(int id) {
		//调用Dao层的方法
		ProductEntity p=productMapper.selectProduct(id);
		if(p.getProductname().equals("huawei")){
			return true;
		}
		return false;
	}
}

编写Controller层

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

import com.cqust.service.ProductService;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@GetMapping("/toFindProduct")
	public String toFindProduct(){
		return "product";
	}
	@PostMapping("/findProduct")
	@ResponseBody	 //该注解用于转换到Jackson串
	public String findProduct(int id){
		boolean rtn=productService.validProduct(id);
		if(rtn == true){
			return "Yes";
		}
		return "No";	
	}
}

jsp页面编写

body部分

<body>
	<form action="${pageContext.request.contextPath }/findProduct" method="post">
    	ID: <input type="text" name="id" >
    	<input type="submit" value="校验" >
  </form>
</body>

你可能感兴趣的:(JavaEE之SSM,java-ee,java,spring,ssm)