SSM项目笔记
来源:互联网 发布:java quartz 实例 编辑:程序博客网 时间:2024/05/12 22:31
1.分析功能及数据库,并确定对象之间的关系(用户,管理员,图书信息,图书类别,订单,订单项)
2.利用逆向生成工具generator 生成DAO,Model,Mapper,生成的对象可能不合适,可以修改
{项目整合
3.新建web工程,添加Maven支持
4.把generator生成的文件拷贝到src/main/java中,并在src/main/java下创建syy.service包(业务逻辑层)和syy.controller层
5.在pom.xml中粘贴所需的依赖包
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- spring版本号 --> <spring.version>4.2.3.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.0</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.21</slf4j.version> <log4j.version>1.2.17</log4j.version></properties> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis的配置 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.20</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>1.2.2</version> </dependency>
6.配置资源文件,包括数据库属性文件,日志文件,spring.xml, spring-mybatis.xml, spring-mvc.xml
在src/main/resource创建conf文件夹,用来放配置文件
a)jdbc.properties
jdbc_driverClassName=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://localhost:3306/dbshopjdbc_username=rootjdbc_password=123456
b)
log4j.rootLogger=DEBUG,Console,Filelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.Target=System.outlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=[%c]%m%nlog4j.appender.File=org.apache.log4j.RollingFileAppender log4j.appender.File.File=mybatis.loglog4j.appender.File.MaxFileSize=10MBlog4j.appender.File.Threshold=ALLlog4j.appender.File.layout=org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
c)Spring.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:conf/jdbc.properties"/> <!-- 扫描文件(自动将servicec层注入) --> <context:component-scan base-package="whq.service"/></beans>
d)spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="syy.controller" /> <mvc:annotation-driven /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <!-- 静态资源加载 --> <mvc:default-servlet-handler/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding"> <value>UTF-8</value> </property> <property name="maxUploadSize"> <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 --> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean>
e)spring-mybatis
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" > <property name="driverClassName"> <value>${jdbc_driverClassName}</value> </property> <property name="url"> <value>${jdbc_url}</value> </property> <property name="username"> <value>${jdbc_username}</value> </property> <property name="password"> <value>${jdbc_password}</value> </property> <!-- 连接池最大使用连接数 --> <property name="maxActive"> <value>20</value> </property> <!-- 初始化连接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 获取连接最大等待时间 --> <property name="maxWait"> <value>60000</value> </property> <!-- 连接池最大空闲 --> <property name="maxIdle"> <value>20</value> </property> <!-- 连接池最小空闲 --> <property name="minIdle"> <value>3</value> </property> <!-- 自动清除无用连接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout"> <value>180</value> </property> <!-- 连接属性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> </bean> <!-- myBatis文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath:whq/mapping/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="whq.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/></beans>
7.配置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" id="WebApp_ID" version="3.1"> <display-name>shopping</display-name> <!-- 读取spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring.xml, classpath:conf/spring-mybatis.xml </param-value> </context-param> <!-- 设计路径变量值 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>springmvc.root</param-value> </context-param> <!-- Spring字符集过滤器 --> <filter> <filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 日志记录 --> <context-param> <!-- 日志配置文件路径 --> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.properties</param-value> </context-param> <context-param> <!-- 日志页面的刷新间隔 --> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC核心配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring-mvc.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
8.编写代码
service层:创建接口IAdminService
public interface IAdminService { public void addAdmin(Admin admin); public Admin getAdmin(Integer id); public List<Admin> findAll(); public void updateAdmin(Admin admin); public Admin getAdminName(Admin admin);}
实现service层接口,创建AdminServiceImpl
@Servicepublic class AdminServiceImpl implements IAdminService { @Resource private AdminMapper adminMapper; @Override public void addAdmin(Admin admin) { adminMapper.insert(admin); } @Override public Admin getAdmin(Integer id) { return adminMapper.selectByPrimaryKey(id); } @Override public List<Admin> findAll() { return adminMapper.getAll(); } @Override public void updateAdmin(Admin admin) { adminMapper.updateByPrimaryKey(admin); } @Override public Admin getAdminName(Admin admin) { return adminMapper.selectByAdminName(admin); }}
9.编写控制层代码,创建AdminController类
@Controllerpublic class AdminController { private AdminServiceImpl adminservice; public AdminServiceImpl getAdminservice() { return adminservice; } @Autowired public void setAdminservice(AdminServiceImpl adminservice) { this.adminservice = adminservice; } @RequestMapping(value="/login",method=RequestMethod.POST) public String login(@RequestParam String adminname,@RequestParam String password) { //System.out.println(adminname); String str=""; Admin a1=new Admin(); Admin a2=new Admin(); a1.setAdminname(adminname); a1.setPassword(password); a2=adminservice.getAdminName(a1); if(a2!=null) { if(a2.getRoot().equals("A")) { str="indexback"; }else if(a2.getRoot().equals("B")) { str=""; } else { str=""; } } else { str="login"; } return str; } @RequestMapping(value="/addAdmin", method=RequestMethod.GET) public String addAdmin(Model model) { model.addAttribute(new Admin()); return "addAdmin"; } @RequestMapping(value="/addAdmin", method=RequestMethod.POST) public String addAdmin(Admin admin,Model model) { adminservice.addAdmin(admin); return "redirect:/listAdmin"; } @RequestMapping(value="/listAdmin") public String listAdmin(Model model) { List<Admin> info=new ArrayList<Admin>(); info=adminservice.findAll(); //是把查询到的数据绑定到request域 model.addAttribute("info",info); return "listAdmin"; } @RequestMapping(value="/updateAdmin/{id}",method=RequestMethod.GET) public String updateAdmin(@PathVariable Integer id,Model model) { //Admin admin=new Admin(); model.addAttribute(adminservice.getAdmin(id)); return "updateAdmin"; } @RequestMapping(value="/updateAdmin/{id}",method=RequestMethod.POST) public String updateAdmin(@PathVariable Integer id,Admin admin,Model model) { //Admin admin=new Admin(); adminservice.updateAdmin(admin); return "redirect:/listAdmin"; }}
10.编写jsp页面
add.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><body> <h4 style="margin-top:20px; color:blue; text-align:right;margin-right:5%;">信息增加</h4> <hr color="#ccc" style="margin-top:-20px;"> <center> <form:form method="post" modelAttribute="admin"> 用户名:<form:input path="adminname"/><br> 密码:<form:input path="password"/><br> 权限:<form:input path="root"/><br> 昵称:<form:input path="nickname"/><br> <input type="submit" value="保存"> </form:form> </center> </body>
list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <body> <h4 style="margin-top:20px; color:blue; text-align:right;margin-right:5%;">信息列表</h4> <hr color="#ccc" style="margin-top:-20px;"> <table width="80%" border="1" align="center"> <tr> <td>编号</td> <td>用户名</td> <td>密码</td> <td>权限</td> <td>昵称</td> <td>修改</td> <td>删除</td> </tr> <c:forEach items="${info }" var="infoad"> <tr> <td>${infoad.id }</td> <td><c:out value="${infoad.adminname }"></c:out></td> <td><c:out value="${infoad.password }"></c:out></td> <td><c:out value="${infoad.root }"></c:out></td> <td><c:out value="${infoad.nickname }"></c:out></td> <td><a href="updateAdmin/${infoad.id }">修改</a></td> <td><a href="removeAdmin/${infoad.id }">删除</a></td> </tr> </c:forEach> </table> </body>
update.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <body> <h4>信息修改</h4> <hr color="#ccc"> <form:form method="post" modelAttribute="admin"> 用户名:<form:input path="adminname"/><br> 密码:<form:input path="password"/><br> 权限:<form:input path="root"/><br> 昵称:<form:input path="nickname"/><br> <input type="submit" value="保存"> </form:form> </body>
用户regist.jsp
<script type="text/javascript" src="js/jquery-2.2.0.min.js"></script> <script type="text/javascript"> $(function(){ $("#username").change(function(){ var val=$(this).val(); val=$.trim(val); if(val=='' || val==null) { alert('用户名不能为空!'); } $.ajax({ type:'get', contentType:'application/x-www-form-urlencoded;charset=utf-8', url:'testuser', data:'username='+val, success:function(data){ if(data.yes=='yes') { $('#message').html('<font color="red">'+'用户名已经存在!'+'</font>'); } else { $('#message').html('<font color="green">'+'用户名可以使用!'+'</font>'); } } }); }); }); </script> </head> <body> <h4 style="margin-top:20px; color:blue; text-align:right;margin-right:5%;">信息增加</h4> <hr color="#ccc" style="margin-top:-20px;"> <center> <form:form method="post" modelAttribute="user"> 用户名:<form:input path="username" id="username"/><span id="message"></span><br> 密码:<form:input path="password"/><br> 地址:<form:input path="address"/><br> 电话:<form:input path="tel"/><br> email:<form:input path="email"/><br> <input type="submit" value="保存"> </form:form> </center> </body>
用户注册的contoller
@Controllerpublic class UserController { private UserServiceImpl userService; public UserServiceImpl getUserService() { return userService; } @Autowired public void setUserService(UserServiceImpl userService) { this.userService = userService; } @RequestMapping(value="/register",method=RequestMethod.GET) public String addUser(Model model) { model.addAttribute(new User()); return "register"; } @RequestMapping(value="/testuser",method=RequestMethod.GET) public void testuser (User user,HttpServletRequest req,HttpServletResponse rep) throws IOException { String str=""; User u1=new User(); u1=userService.findid(user); if(u1!=null) { str="{\"yes\":\"yes\"}"; } else { str="{\"yes\":\"no\"}"; } rep.setContentType("application/json;charset=utf-8"); rep.getWriter().write(str); } @RequestMapping(value="/register",method=RequestMethod.POST) public String addUser(User user,Model model) { userService.addUser(user); return "success"; }}
一对一关系:
1.新建一个扩展类Model:OrderUser,一个订单对一个用户
//扩展类public class OrderUser extends Order { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }}
2.创建OrderUserMapper接口
public interface OrderUserMapper { public List<OrderUser> orderselectall(); }
3.创建orderuserMapper.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="whq.dao.OrderUserMapper" > <resultMap id="BaseResultMap" type="syy.model.Order" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="orderid" property="orderid" jdbcType="VARCHAR" /> <result column="userid" property="userid" jdbcType="INTEGER" /> <result column="note" property="note" jdbcType="VARCHAR" /> <result column="createtime" property="createtime" jdbcType="TIMESTAMP" /> </resultMap> <resultMap type="syy.model.OrderUser" id="OrderUsermap"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="orderid" property="orderid" jdbcType="VARCHAR" /> <result column="userid" property="userid" jdbcType="INTEGER" /> <result column="note" property="note" jdbcType="VARCHAR" /> <result column="createtime" property="createtime" jdbcType="TIMESTAMP" /> <association property="user" javaType="whq.model.User"> <id column="userid" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="address" property="address"/> <result column="tel" property="tel"/> <result column="email" property="email"/> </association> </resultMap> <select id="orderselectall" resultMap="OrderUsermap"> SELECT torder.*,tuser.* from torder,tuser where torder.userid=tuser.id </select></mapper>
0 0
- SSM项目笔记
- Maven与SSM项目整合学习笔记
- SSM框架搭建项目整合学习笔记
- IDEA下SSM框架项目笔记
- IDEA下SSM框架项目笔记三
- SSM初次学习后的项目笔记
- SSM项目整合笔记-原项目重构
- SSM项目整合笔记-Maven使用Jetty运行JavaWeb项目
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- SSM项目整合笔记-Eclipse搭建Maven开发环境
- SSM的maven项目
- Maven创建SSM项目
- ssm范例项目
- ssm项目集成步骤
- ssm项目搭建
- Myeclipse破解版添加外部服务器
- 自定义元素属性,以及用jquery 去获取当前点击的元素的这个自定义属性
- java集合框架
- tomcat的原理
- UVA 1588-Kickdown
- SSM项目笔记
- 如何将int型数据放到字典里
- visual studio cmakelists
- XML中DTD笔记
- Spring+Websocket实现消息的推送
- JDBC
- swift实现删除图片时的抖动动画
- POJ 2051 Argus
- php mysql in array