demo_1
程序员文章站
2022-04-16 08:44:09
我练习的demo是基于SSM+MySQL+Eclipse+Tomcat8+Maven3实现的; 创建项目 ## 创建Maven Project: Artifact Id: cn.com.demo Group Id: demo ## 完成项目的基本配置 ## 生成web.xml ## 添加Tomcat ......
我练习的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>
上一篇: 初学Python——函数