demo_1
Posted debugtheworld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了demo_1相关的知识,希望对你有一定的参考价值。
我练习的demo是基于SSM+mysql+Eclipse+Tomcat8+Maven3实现的;
创建项目
## 创建Maven Project:
Artifact Id: cn.com.demo
Group Id: demo
## 完成项目的基本配置
## 生成web.xml
## 添加Tomcat Runtime
## 添加pom.xml
1 <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">
2 <modelVersion>4.0.0</modelVersion>
3 <groupId>cn.xx</groupId>
4 <artifactId>demo</artifactId>
5 <version>0.0.1-SNAPSHOT</version>
6 <packaging>war</packaging>
7 <dependencies>
8 <!-- spring 的依赖jar包 -->
9 <dependency>
10 <groupId>org.springframework</groupId>
11 <artifactId>spring-webmvc</artifactId>
12 <version>4.3.9.RELEASE</version>
13 </dependency>
14
15 <!-- spring-jdbc的依赖jar包 -->
16 <dependency>
17 <groupId>org.springframework</groupId>
18 <artifactId>spring-jdbc</artifactId>
19 <version>4.3.9.RELEASE</version>
20 </dependency>
21
22 <!-- junit测试jar包 -->
23 <dependency>
24 <groupId>junit</groupId>
25 <artifactId>junit</artifactId>
26 <version>4.12</version>
27 </dependency>
28
29 <!-- 数据库的连接池 -->
30 <dependency>
31 <groupId>commons-dbcp</groupId>
32 <artifactId>commons-dbcp</artifactId>
33 <version>1.4</version>
34 </dependency>
35
36 <!-- mysql数据库 -->
37 <dependency>
38 <groupId>MySQL</groupId>
39 <artifactId>mysql-connector-java</artifactId>
40 <version>5.1.6</version>
41 </dependency>
42
43 <!-- mybatis -->
44 <dependency>
45 <groupId>org.mybatis</groupId>
46 <artifactId>mybatis</artifactId>
47 <version>3.2.5</version>
48 </dependency>
49
50 <!-- mybatis-spring整合 -->
51 <dependency>
52 <groupId>org.mybatis</groupId>
53 <artifactId>mybatis-spring</artifactId>
54 <version>1.3.2</version>
55 </dependency>
56
57 <!-- jstl -->
58 <dependency>
59 <groupId>jstl</groupId>
60 <artifactId>jstl</artifactId>
61 <version>1.2</version>
62 </dependency>
63
64 </dependencies>
65 </project>
## 配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 version="2.5">
6 <display-name>demo</display-name>
7
8 <welcome-file-list>
9 <welcome-file>index.html</welcome-file>
10 <welcome-file>index.jsp</welcome-file>
11 </welcome-file-list>
12 <!-- Servlet控制器 -->
13 <servlet>
14 <servlet-name>dispatcherServlet</servlet-name>
15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16 <init-param>
17 <param-name>contextConfigLocation</param-name>
18 <param-value>classpath:spring-*.xml</param-value>
19 </init-param>
20 <load-on-startup>1</load-on-startup>
21 </servlet>
22 <servlet-mapping>
23 <servlet-name>dispatcherServlet</servlet-name>
24 <url-pattern>*.do</url-pattern>
25 </servlet-mapping>
26
27 <filter>
28 <filter-name>filter</filter-name>
29 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
30 <init-param>
31 <param-name>encoding</param-name>
32 <param-value>utf-8</param-value>
33 </init-param>
34 </filter>
35 <filter-mapping>
36 <filter-name>filter</filter-name>
37 <url-pattern>/*</url-pattern>
38 </filter-mapping>
39 </web-app>
## Spring的配置文件
spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
6 xmlns:jee="http://www.springframework.org/schema/jee"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xmlns:aop="http://www.springframework.org/schema/aop"
9 xmlns:mvc="http://www.springframework.org/schema/mvc"
10 xmlns:util="http://www.springframework.org/schema/util"
11 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12 xsi:schemaLocation="
13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
22
23 <!-- 组件扫描 -->
24 <context:component-scan base-package="控制器路径"/>
25
26 <!-- 配置视图解析器 InternalResourceViewResolver -->
27 <bean id="ViewResource" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
28 <!-- 前缀 -->
29 <property name="prefix" value="/WEB-INF/web/"/>
30 <!-- 后缀 -->
31 <property name="suffix" value=".jsp"/>
32
33 <!-- 1、基于注解的映射器默认是:DefaultAnnotationHandlerMapping,映射处理器是2.5版本的;
34 2、3.2版本定义一个新的映射处理器:RequestMappingHandlerMapping
35 3、如果要改变默认的映射处理器,处理下面的配置
36 4、默认初始化一些工具类:比如异常处理,解析json-->
37 <mvc:annotation-driven />
38 </bean>
39
40
41
42
43
44
45
46 </beans>
spring-dao.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
6 xmlns:jee="http://www.springframework.org/schema/jee"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xmlns:aop="http://www.springframework.org/schema/aop"
9 xmlns:mvc="http://www.springframework.org/schema/mvc"
10 xmlns:util="http://www.springframework.org/schema/util"
11 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12 xsi:schemaLocation="
13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
22
23 <!--1、util:properties表示读取外部的属性文件,并实例化对象
24 2、id表示名称
25 3、localhost表示属性文件的位置 -->
26 <util:properties id="jdbc"
27 location="classpath:db.properties" />
28
29 <!-- 配置数据库的连接池
30 1、使用spring表达式给属性赋值
31 2、spring表达式语法格式:#{} -->
32 <bean id="dataSource"
33 class="org.apache.commons.dbcp.BasicDataSource">
34 <property name="driverClassName"
35 value="#{jdbc.driverClassName}" />
36 <property name="url" value="#{jdbc.url}" />
37 <property name="username" value="#{jdbc.username}" />
38 <property name="password" value="#{jdbc.password}" />
39 </bean>
40
41 <!-- 持久层接口的扫描 -->
42 <bean id="scannerConfigurer"
43 class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44 <property name="basePackage" value="持久层路径" />
45 </bean>
46
47 <!-- SqlSessionFactoryBean的初始化 -->
48 <bean id="factoryBean"
49 class="org.mybatis.spring.SqlSessionFactoryBean">
50 <!-- 依赖注入数据源(dataSource) -->
51 <property name="dataSource" ref="dataSource" />
52 <!-- 读取编写sql语句的映射文件 -->
53 <property name="mapperLocations"
54 value="classpath:mappers/*.xml" />
55 </bean>
56
57 </beans>
spring-service.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
6 xmlns:jee="http://www.springframework.org/schema/jee"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xmlns:aop="http://www.springframework.org/schema/aop"
9 xmlns:mvc="http://www.springframework.org/schema/mvc"
10 xmlns:util="http://www.springframework.org/schema/util"
11 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
12 xsi:schemaLocation="
13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
22
23 <!-- 扫描包 可以扫描到当前包和子包下的所有类 -->
24 <context:component-scan
25 base-package="cn.com.service" />
26
27
28 </beans>
## 数据库的配置文件
db.properties
1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/mysql 3 username=root 4 password=123
## MyBatis的映射模版
创建mappers文件夹,并添加sql映射文件
持久层
检查配置
检查`db.properties`中的数据库名称
检查`spring-dao.xml`中配置的接口文件的包名
创建User类
创建`com.demo.pojo.User`类,属性可参考数据表。
私有化所有属性,提供所有属性的SET/GET方法,添加无参数和全参数的构造方法,自动生成`toString()`方法(便于测试数据),`equals()`和`hashCode()`可后续再添加,实现`Serializable`接口,并生成序列化ID。
1 package com.demo.pojo; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 public class User implements Serializable { 7 /** 8 * 9 */ 10 private static final long serialVersionUID = -4390901662089334130L; 11 private Integer id; 12 private String username; 13 private String password; 14 private Integer gender; 15 private String phone; 16 private String email; 17 private String uuid; 18 private String createdUser; 19 private Date createdTime; 20 private String modifiedUser; 21 private Date modifiedTime; 22 23 public User() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 public User(Integer id, String username, String password, Integer gender, String phone, String email, String uuid, 29 String createdUser, Date createdTime, String modifiedUser, Date modifiedTime) { 30 super(); 31 this.id = id; 32 this.username = username; 33 this.password = password; 34 this.gender = gender; 35 this.phone = phone; 36 this.email = email; 37 this.uuid = uuid; 38 this.createdUser = createdUser; 39 this.createdTime = createdTime; 40 this.modifiedUser = modifiedUser; 41 this.modifiedTime = modifiedTime; 42 } 43 44 public Integer getId() { 45 return id; 46 } 47 48 public void setId(Integer id) { 49 this.id = id; 50 } 51 52 public String getUsername() { 53 return username; 54 } 55 56 public void setUsername(String username) { 57 this.username = username; 58 } 59 60 public String getPassword() { 61 return password; 62 } 63 64 public void setPassword(String password) { 65 this.password = password; 66 } 67 68 public Integer getGender() { 69 return gender; 70 } 71 72 public void setGender(Integer gender) { 73 this.gender = gender; 74 } 75 76 public String getPhone() { 77 return phone; 78 } 79 80 public void setPhone(String phone) { 81 this.phone = phone; 82 } 83 84 public String getEmail() { 85 return email; 86 } 87 88 public void setEmail(String email) { 89 this.email = email; 90 } 91 92 public String getUuid() { 93 return uuid; 94 } 95 96 public void setUuid(String uuid) { 97 this.uuid = uuid; 98 } 99 100 public String getCreatedUser() { 101 return createdUser; 102 } 103 104 public void setCreatedUser(String createdUser) { 105 this.createdUser = createdUser; 106 } 107 108 public Date getCreatedTime() { 109 return createdTime; 110 } 111 112 public void setCreatedTime(Date createdTime) { 113 this.createdTime = createdTime; 114 } 115 116 public String getModifiedUser() { 117 return modifiedUser; 118 } 119 120 public void setModifiedUser(String modifiedUser) { 121 this.modifiedUser = modifiedUser; 122 } 123 124 public Date getModifiedTime() { 125 return modifiedTime; 126 } 127 128 public void setModifiedTime(Date modifiedTime) { 129 this.modifiedTime = modifiedTime; 130 } 131 132 @Override 133 public String toString() { 134 return "User [id=" + id + ", username=" + username + ", password=" + password + ", gender=" + gender 135 + ", phone=" + phone + ", email=" + email + ", uuid=" + uuid + ", createdUser=" + createdUser 136 + ", createdTime=" + createdTime + ", modifiedUser=" + modifiedUser + ", modifiedTime=" + modifiedTime 137 + "]"; 138 } 139 140 }
创建包和接口
创建`com.demo.dao.UserMapper`接口,并添加抽象方法:
1 package com.demo.dao; 2 3 import com.demo.pojo.User; 4 5 public interface UserMapper { 6 /** 7 * 添加用户信息 8 * @param user 用户信息 9 * @return 返回有效行数 10 */ 11 Integer insert(User user); 12 }
## 配置XML映射
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 3 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 4 5 <!-- namespace:匹配的接口 --> 6 <mapper namespace="com.demo.dao.UserMapper"> 7 8 <!-- 添加用户信息 --> 9 <!-- Integer insert(User user) --> 10 11 <insert id="insert" parameterType="com.demo.pojo.User" 12 useGeneratedKeys="true" keyProperty="id"> 13 INSERT INTO 14 t_user ( 15 username, 16 password, 17 gender, 18 phone, 19 email, 20 uuid, 21 created_user, 22 created_time, 23 modified_user, 24 modified_time 25 ) VALUES ( 26 #{username}, 27 #{password}, 28 #{gender}, 29 #{phone}, 30 #{email}, 31 #{uuid}, 32 #{createdUser}, 33 #{createdTime}, 34 #{modifiedUser}, 35 #{modifiedTime} 36 ) 37 </insert> 38 </mapper>
以上是关于demo_1的主要内容,如果未能解决你的问题,请参考以下文章
12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段