04.AdminLTE的基本介绍
05.SSM整合案例的基本介绍
06.产品操作
07.订单操作
08.用户操作
09.权限控制
10.权限关联与控制
11.AOP日志
06.产品操作
SSM 环境搭建与产品操作
1.数据库与表结构介绍
PLSQL Developer 13 是一个集成开发环境,专门面向Oracle数据库存储程序单元的开发PL/SQL Developer侧重于易用性、代码品质和生产力 。
连接数据库的配置
解决中文??乱码
在maven bin目录下运行以下命令:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.1.0 -Dpackaging=jar -Dfile=D:\mnt\ojdbc14-10.2.0.1.0.jar以上地址信息部分,请根据本地jar包的信息自行修改。
-- 用户名 ssm08 -- 登录密码 itheima
-- Create the user create user SSM08 identified by itheima; -- Grant/Revoke role privileges grant connect to SSM08; grant resource to SSM08;
产品表信息描述
CREATE TABLE product( id varchar2(32) default SYS_GUID() PRIMARY KEY, productNum VARCHAR2(50) NOT NULL, productName VARCHAR2(50), cityName VARCHAR2(50), DepartureTime timestamp, productPrice Number, productDesc VARCHAR2(500), productStatus INT, CONSTRAINT product UNIQUE (id, productNum) ) insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice,productdesc, productstatus) values ('676C5BD1D35E429A8C2E114939C5685A', 'itcast-002', '北京三日游', '北京', to_timestamp('10-10-2018 10:10:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 1200, '不错的旅行', 1); insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice,productdesc, productstatus) values ('12B7ABF2A4C544568B0A7C69F36BF8B7', 'itcast-003', '上海五日游', '上海', to_timestamp('25-04-2018 14:30:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 1800, '魔都我来了', 0); insert into PRODUCT (id, productnum, productname, cityname, departuretime, productprice,productdesc, productstatus) values ('9F71F01CB448476DAFB309AA6DF9497F', 'itcast-001', '北京三日游', '北京', to_timestamp('10-10-2018 10:10:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 1200, '不错的旅行', 1);
2.SSM环境搭建maven工程
使用Maven管理项目,创建parent工程和子模块。
解决maven项目创建archetype过慢的问题:
archetypeCatalog internal
heima_ssm_parent父工程的
pom.xml
xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.bjutgroupId>
<artifactId>heima_ssmartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>5.0.15.RELEASEspring.version>
<slf4j.version>1.7.12slf4j.version>
<log4j.version>1.2.17log4j.version>
<shiro.version>1.2.3shiro.version>
<mybatis.version>3.5.2mybatis.version>
<mysql.version>8.0.17mysql.version>
<oracle.version>10.2.0.1.0oracle.version>
<spring.security.version>5.0.10.RELEASEspring.security.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
<type>jartype>
<scope>compilescope>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.10version>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-webartifactId>
<version>${spring.security.version}version>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-configartifactId>
<version>${spring.security.version}version>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-coreartifactId>
<version>${spring.security.version}version>
dependency>
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-taglibsartifactId>
<version>${spring.security.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.9version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc14artifactId>
<version>${oracle.version}version>
dependency>
<dependency>
<groupId>javax.annotationgroupId>
<artifactId>jsr250-apiartifactId>
<version>1.0version>
dependency>
dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.6.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
<showWarnings>trueshowWarnings>
configuration>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
plugin>
plugins>
build>
<modules>
<module>heima_ssm_daomodule>
<module>heima_ssm_domainmodule>
<module>heima_ssm_utilsmodule>
<module>heima_ssm_servicemodule>
<module>heima_ssm_webmodule>
modules>
project>
1.3在domain编写实体类
package cn.bjut.ssm.domain; import java.util.Date; /** * 商品信息 */ public class Product { private String id; // 主键 private String productNum; // 编号 唯一 private String productName; // 名称 private String cityName; // 出发城市 private Date departureTime; // 出发时间 private String departureTimeStr; //方便网页展示,数据库里没有对应字段 private double productPrice; // 产品价格 private String productDesc; // 产品描述 private Integer productStatus; // 状态 0 关闭 1 开启 private String productStatusStr; //方便网页展示,数据库里没有对应字段
实体类的pom.xml添加对后面步骤编写的工具类的依赖坐标
<dependencies> <dependency> <groupId>cn.bjutgroupId> <artifactId>heima_ssm_utilsartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies>
选择子模块heima_ssm_dao ,在它的pom.xml里引入兄弟模块的依赖坐标。
<dependencies> <dependency> <groupId>cn.bjutgroupId> <artifactId>heima_ssm_domainartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies>
新建两个package在java&resources目录下
cn.bjut.ssm.dao
新建一个接口,IProductDao
package cn.bjut.ssm.dao; import cn.bjut.ssm.domain.Product; import java.util.List; public interface IProductDao { //查询所有的产品信息 public ListfindAll() throws Exception; }
选择子模块heima_ssm_service ,在它的pom.xml里引入兄弟模块(dao层)的依赖坐标。
<dependencies> <dependency> <groupId>cn.bjutgroupId> <artifactId>heima_ssm_daoartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies>
在java目录下新建一个package ,在里面新建一个实现类的包impl
cn.bjut.ssm.service
编写接口 IProductService
package cn.bjut.ssm.service; import cn.bjut.ssm.domain.Product; import java.util.List; public interface IProductService { //查询所有的产品信息 public ListfindAll() throws Exception; }
编写接口的实现类 ProductServiceImpl
@Service @Transactional //注解的方式配置spring事务管理 public class ProductServiceImpl implements IProductService { @Autowired private IProductDao productDao; @Override public ListfindAll()throws Exception{ return productDao.findAll(); } }
1.SSM环境搭建与产品操作.pdf 里面是用mybatis的注解方式查询操作数据库,现在我选择用.xml配置文件的方式使用mybatis
MyBatis从入门到精通(第3章): MyBatis注解方式的基本使用
MyBatis从入门到精通(第2章):MyBatis XML方式的基本用法
在 heima_ssm_dao 子模块的resources目录下,新建一个cn.bjut.ssm.dao包(package),然后在其中新建一个文件:
IProductDao.xml 【如果是mybatis整合spring框架,这里的.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" >
namespace属性的值为:接口的全限定类名。resultType是实体类的全限定类名。【测试后发现整合spring框架并且分父子工程依赖模块,resultType就必须写全限定类名】数据库的字段名放前面,实体类的成员变量名放后面。
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="cn.bjut.ssm.dao.IProductDao">
<select id="findAll()" resultType="cn.bjut.ssm.domain.Product">
select id,
productNum productNum,
productName productName,
cityName cityName,
DepartureTime DepartureTime,
productPrice productPrice,
productDesc productDesc,
productStatus productStatus
from product
select>
mapper>
下面将引入框架用到的各种配置文件
db.properties
\day02\资料
heima_ssm_dao子模块的resources目录
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@192.168.0.104:1521:orcl jdbc.username=ssm08 jdbc.password=itheima
ip地址可以通过ipconfig命令在cmd中获得,1521是oracle数据库的默认端口号。
log4j.properties
heima_ssm_web子模块的resources目录
# Set root category priority to INFO and its only appender to CONSOLE. #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal log4j.rootCategory=debug, CONSOLE, LOGFILE # Set the enterprise logger category to FATAL and its only appender to CONSOLE. log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n # LOGFILE is set to be a File appender using a PatternLayout. # log4j.appender.LOGFILE=org.apache.log4j.FileAppender # log4j.appender.LOGFILE.File=d:\axis.log # log4j.appender.LOGFILE.Append=true # log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout # log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
applicationContext-dao.xml
heima_ssm_dao子模块的resources目录下新建的spring目录
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">
使用c3p0数据库连接池
<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>
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:property-placeholder location="classpath:db.properties" />
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
<property name="typeAliasesPackage" value="cn.bjut.ssm.domian"/>
bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.bjut.ssm.dao"/>
bean>
beans>
sqlMapConfig.xml
heima_ssm_dao子模块的resources目录下新建的mybatis目录的里面。
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">
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>
<settings>
<setting name="logImpl" value="LOG4J"/>
settings>
configuration>
在heima_ssm_service子模块的resources目录下,新建一个Directory目录名为spring里面放着
applicationContext-trans.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="cn.bjut.ssm.service"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pointcut1" expression="execution(* cn.bjut.ssm.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
aop:config>
beans>
applicationContext-trans.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="cn.bjut.ssm.dao"/>
<context:component-scan base-package="cn.bjut.ssm.service"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
beans>
web子模块的resources目录
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: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">
<import resource="classpath:spring/applicationContext-dao.xml"/>
<import resource="classpath:spring/applicationContext-service.xml"/>
beans>
webapp\WEB-INF目录下
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">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
springmvc.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="cn.bjut.ssm.controller"/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/plugins/" mapping="/plugins/**" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>主页title> head> <body> <a href="${pageContext.request.contextPath}/product/findAll.do">查询所有的产品信息a> body> html>
把静态资源文件夹 css、img、plugins文件夹拷贝到webapp目录下;
把pages文件夹放入 WEB-INF目录下。
最后开始编写web子模块的controller类:
ProductController
package cn.bjut.ssm.controller; import cn.bjut.ssm.service.IProductService; import cn.bjut.ssm.domain.Product; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller @RequestMapping("/product") public class ProductController { @Autowired private IProductService productService; //产品添加 // @RequestMapping("/save.do") // public String save(Product product) throws Exception { // productService.save(product); // return "redirect:findAll.do"; // } //查询全部产品 @RequestMapping("/findAll.do") public ModelAndView findAll() throws Exception { ModelAndView mv = new ModelAndView(); Listps = productService.findAll(); mv.addObject("productList", ps); mv.setViewName("product-list1"); return mv; } }
回到heima_ssm_utils这个子模块下,新建一个工具类。
cn.bjut.ssm.utils.DateUtils
package cn.bjut.ssm.utils; import java.text.ParseException; // import java.text.SimpleDateFormat; import java.util.Date; public class DateUtils { //日期转换成字符串 public static String date2String(Date date, String patt) { SimpleDateFormat sdf = new SimpleDateFormat(patt); String format = sdf.format(date); return format; } //字符串转换成日期 public static Date string2Date(String str, String patt) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(patt); Date parse = sdf.parse(str); return parse; } }
heima_ssm_web的pom.xml里面可以添加一个Tomcat7的插件。
xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>heima_ssmartifactId>
<groupId>cn.bjutgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>heima_ssm_webartifactId>
<packaging>warpackaging>
<name>heima_ssm_web Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>cn.bjutgroupId>
<artifactId>heima_ssm_serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<finalName>heima_ssm_webfinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.0.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.7.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.20.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.0version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8888port>
configuration>
plugin>
plugins>
build>
project>
使用IDEA并且指定Tomcat7插件测试运行的 网页访问路径如下
http://localhost:8888/heima_ssm_web/
6.产品操作-查询全部产品3
根据.jsp页面的需要,重新编辑cn.bjut.ssm.domain
Product.java
package cn.bjut.ssm.domain; import cn.bjut.ssm.utils.DateUtils; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * 商品信息 */ public class Product { private String id; // 主键 private String productNum; // 编号 唯一 private String productName; // 名称 private String cityName; // 出发城市 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm") private Date departureTime; // 出发时间 private String departureTimeStr; //为了页面显示,数据库里没有的字段 private double productPrice; // 产品价格 private String productDesc; // 产品描述 private Integer productStatus; // 状态 0 关闭 1 开启 private String productStatusStr; //为了页面显示,数据库里没有的字段 public String getDepartureTimeStr() { if(departureTime!=null){ departureTimeStr= DateUtils.date2String(departureTime,"yyyy-MM-dd HH:mm:ss"); } return departureTimeStr; } public void setDepartureTimeStr(String departureTimeStr) { this.departureTimeStr = departureTimeStr; } public String getProductStatusStr() { if (productStatus != null) { // 状态 0 关闭 1 开启 if(productStatus==0) productStatusStr="关闭"; if(productStatus==1) productStatusStr="开启"; } return productStatusStr; } public void setProductStatusStr(String productStatusStr) { this.productStatusStr = productStatusStr; } //===============================================================================// public String getId() { return id; } public void setId(String id) { this.id = id; } public String getProductNum() { return productNum; } public void setProductNum(String productNum) { this.productNum = productNum; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public Date getDepartureTime() { return departureTime; } public void setDepartureTime(Date departureTime) { this.departureTime = departureTime; } public double getProductPrice() { return productPrice; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } public String getProductDesc() { return productDesc; } public void setProductDesc(String productDesc) { this.productDesc = productDesc; } public Integer getProductStatus() { return productStatus; } public void setProductStatus(Integer productStatus) { this.productStatus = productStatus; } //========================================================================================// }
HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> <property name="mapperLocations" value="classpath:cn/bjut/ssm/dao/*.xml"/> bean> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bjut.ssm.dao"/> bean>此时测试SSM项目会提示报错,理由是找不到findall()方法。因为讲师用的是注解开发mybatis ,而我这里是用:接口类+.xml配置文件的方式。
前面在给oracle数据库创建users ssm08时候,没有指定tablespace,会导致后面没有权限查到数据表。ITHEIMA是DBA权限创建PRODUCT表时所在的表空间。
-- alter user username default tablespace userspace alter user ssm08 default tablespace ITHEIMA;
查询全部产品的
ProductController
@Controller @RequestMapping("/product") public class ProductController { @Autowired private IProductService productService; //查询全部产品 @RequestMapping("/findAll.do") public ModelAndView findAll() throws Exception { ModelAndView mv = new ModelAndView(); Listps = productService.findAll(); mv.addObject("productList", ps); mv.setViewName("product-list"); return mv; } }
7.product-list.jsp页面制作
使用IDEA处理JSP页面的脚本内容,替换的快捷键是 Ctrl+R
Except Comments可以排除注释的内容。
../
被替换为
${pageContext.request.contextPath}
如果使用MySQL做数据库,那么语句语法是有不同的要注意。
use itheima; drop table if exists `product`; -- 创建1个表 CREATE TABLE `product`( `id` varchar(32) NOT NULL, `productNum` varchar(50) NOT NULL, `productName` varchar(50), `cityName` varchar(50), `departureTime` timestamp, `productPrice` INT, `productDesc` varchar(500), `productStatus` INT, PRIMARY KEY (`id`) ); -- 插入测试数据 insert into product (id, productNum, productName, cityName, departureTime, productPrice,productDesc, productStatus) values ('676C5BD1D35E429A8C2E114939C5685A', 'itcast-002', '北京三日游', '北京', '2018-10-10 10:10:00', 1200, '不错的旅行', 1); insert into product (id, productNum, productName, cityName, departureTime, productPrice,productDesc, productStatus) values ('9F71F01CB448476DAFB309AA6DF9497F', 'itcast-001', '北京三日游', '北京', '2018-10-10 10:10:00', 1200, '不错的旅行', 1); insert into product (id, productNum, productName, cityName, departureTime, productPrice,productDesc, productStatus) values ('12B7ABF2A4C544568B0A7C69F36BF8B7', 'itcast-003', '上海五日游', '上海', '2019-10-10 14:30:00', 1800, '魔都我来了', 0);
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/itheima?serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root
8.main.jsp页面制作
当前项目默认的index.jsp长得比较简陋,我们希望从index.jsp直接跳转到视觉效果更好的 main.jsp 前端页面。