同美商城(一)
一、商城整体架构
三、数据库设计
四、maven热部署
1、配置多个tomcat
a、配置jdk环境
b、修改端口三个地方
c、创建maven项目,输入命令:tomcat:run(第一次运行需要下载jar)
2、热部署
a、给tomcat配置账号密码,找到tomcat-users.xml文件写入:
<role rolename="manager-gui"/>
<role rolename="manager-script" />
<user username="admin" password="123" roles="manager-gui,manager-script" />
b、配置pom.xml文件,添加插件(第一次运行需要下载jar包)
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8888/manager/text</url>
<path>/</path>
<username>admin</username>
<password>123</password>
</configuration>
</plugin>
</plugins>
c、部署到tomcat:
tomcat7:deploy 存在,则不再重新部署
tomcat7:redeploy 每次重新部署
五、框架搭建
1、配置pox.xml文件,配置项目所需jar
2、配置web.xml:
配置spring监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、创建anotation.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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- spring扫描@service -->
<context:component-scan base-package="cn.tongmei">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:annotation-config/>
</beans>
4、创建jdbc.properties和jdbc.xml文件,并配置:
jdbc.properties文件(由于mysql版本为8.0.11,所以这个配置会稍有不一样)
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/shopping?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
user=root
password=
jdbc.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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
5、配置property.xml文件,读取jdbc配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- jdbc的配置 -->
<value>classpath:properties/jdbc.properties</value>
<!-- memcached的配置 -->
</list>
</property>
</bean>
6、配置mybatis.xml文件 <!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/tongmei/croe/dao/*.xml"/>
<property name="typeAliasesPackage" value="cn.tongmei.croe.bean"></property>
</bean>
<!-- 扫包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tongmei.croe.dao"/>
</bean>
7、事务管理transation.xml:
<!--spring 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
8、UserInfoDAO.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="cn.tongmei.croe.dao.UserInfoDAO">
<insert id="add" parameterType="UserInfo">
insert into user_info (user_name,user_sex)
values(#{userName},#{userSex})
</insert>
</mapper>
9、事务管理注释@Transactional的用法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tongmei.croe.bean.UserInfo;
import cn.tongmei.croe.dao.UserInfoDAO;
import cn.tongmei.croe.service.IUserInfoService;
@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {
@Autowired
private UserInfoDAO userDAO;
public void addUser(UserInfo user){
int i=userDAO.add(user);
System.out.println(i);
throw new RuntimeException("运行异常");
}
}
10、测试类的编写import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.tongmei.croe.bean.UserInfo;
import cn.tongmei.croe.service.IUserInfoService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:application-context.xml"})
public class UserTest {
@Autowired
IUserInfoService service;
@Test
public void testAdd(){
UserInfo user=new UserInfo();
user.setUserName("华安");
user.setUserSex("男");
service.addUser(user);
}
}
六、springmvc配置
1.创建后台配置文件springmvc-back.xml<context:component-scan base-package="cn.liu"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- jsp视图 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/back_page/" />
<property name="suffix" value=".jsp" />
</bean>
2.创建前台配置文件springmvc-front.xml <context:component-scan base-package="cn.liu"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- jsp视图 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/back_page/" />
<property name="suffix" value=".jsp" />
</bean>
3.配置web.xml<!-- springmvc 后台配置 -->
<servlet>
<servlet-name>back</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-back.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>back</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- springmvc 前台配置 -->
<servlet>
<servlet-name>front</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-front.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>front</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
七、springmvc乱码问题
<!-- 过滤器 过滤乱码 -->
<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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
上一篇: linux命令行上传代码至github
下一篇: 《小豪商城》项目总结