PS:以下内容根据老师讲课内容进行的梳理
在src下创建四个包:entity、dao、service、controller
在WebContent下建立文件夹:js, jsp, css, images 等
spring、aop、mybatis、mybatis-spring、dbcp、mysql驱动、lang、jackson等。以下是所有的包
在src下创建以下5个文件
<context:component-scan base-package="com.service"></context:component-scan>
<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>
另:给数据库设置密码
① 点击数据库的用户
② 将这两个点进去 修改密码
③ 点击连接属性 将刚才的新密码填进去
即可完成。
<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>
<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>
<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: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>
完整的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>
主要:别名配置
<?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>
<?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.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
下面两者的命名要一样
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>
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;
}
}
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";
}
}
body部分
<body>
<form action="${pageContext.request.contextPath }/findProduct" method="post">
ID: <input type="text" name="id" >
<input type="submit" value="校验" >
</form>
</body>