我练习的demo是基于SSM+MySQL+Eclipse+Tomcat8+Maven3实现的;

创建项目

##  创建Maven Project:

  Artifact Id: cn.com.demo
       Group Id: demo

##  完成项目的基本配置

##  生成web.xml

##  添加Tomcat Runtime

##  添加pom.xml

 <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.0</modelVersion>
<groupId>cn.xx</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- spring 的依赖jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency> <!-- spring-jdbc的依赖jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency> <!-- junit测试jar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <!-- 数据库的连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <!-- mysql数据库 -->
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency> <!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.5</version>
</dependency> <!-- mybatis-spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> </dependencies>
</project>

##  配置web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>demo</display-name> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Servlet控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <filter>
<filter-name>filter</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>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

##  Spring的配置文件

spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 组件扫描 -->
<context:component-scan base-package="控制器路径"/> <!-- 配置视图解析器 InternalResourceViewResolver -->
<bean id="ViewResource" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/web/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/> <!-- 1、基于注解的映射器默认是:DefaultAnnotationHandlerMapping,映射处理器是2.5版本的;
2、3.2版本定义一个新的映射处理器:RequestMappingHandlerMapping
3、如果要改变默认的映射处理器,处理下面的配置
4、默认初始化一些工具类:比如异常处理,解析json-->
<mvc:annotation-driven />
</bean> </beans>

spring-dao.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!--1、util:properties表示读取外部的属性文件,并实例化对象
2、id表示名称
3、localhost表示属性文件的位置 -->
<util:properties id="jdbc"
location="classpath:db.properties" /> <!-- 配置数据库的连接池
1、使用spring表达式给属性赋值
2、spring表达式语法格式:#{} -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="#{jdbc.driverClassName}" />
<property name="url" value="#{jdbc.url}" />
<property name="username" value="#{jdbc.username}" />
<property name="password" value="#{jdbc.password}" />
</bean> <!-- 持久层接口的扫描 -->
<bean id="scannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="持久层路径" />
</bean> <!-- SqlSessionFactoryBean的初始化 -->
<bean id="factoryBean"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 依赖注入数据源(dataSource) -->
<property name="dataSource" ref="dataSource" />
<!-- 读取编写sql语句的映射文件 -->
<property name="mapperLocations"
value="classpath:mappers/*.xml" />
</bean> </beans>

spring-service.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 扫描包 可以扫描到当前包和子包下的所有类 -->
<context:component-scan
base-package="cn.com.service" /> </beans>

##  数据库的配置文件

db.properties

 driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql
username=root
password=123

##  MyBatis的映射模版

创建mappers文件夹,并添加sql映射文件

持久层

检查配置
检查`db.properties`中的数据库名称
检查`spring-dao.xml`中配置的接口文件的包名

创建User类
创建`com.demo.pojo.User`类,属性可参考数据表。

私有化所有属性,提供所有属性的SET/GET方法,添加无参数和全参数的构造方法,自动生成`toString()`方法(便于测试数据),`equals()`和`hashCode()`可后续再添加,实现`Serializable`接口,并生成序列化ID。

 package com.demo.pojo;

 import java.io.Serializable;
import java.util.Date; public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4390901662089334130L;
private Integer id;
private String username;
private String password;
private Integer gender;
private String phone;
private String email;
private String uuid;
private String createdUser;
private Date createdTime;
private String modifiedUser;
private Date modifiedTime; public User() {
super();
// TODO Auto-generated constructor stub
} public User(Integer id, String username, String password, Integer gender, String phone, String email, String uuid,
String createdUser, Date createdTime, String modifiedUser, Date modifiedTime) {
super();
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.phone = phone;
this.email = email;
this.uuid = uuid;
this.createdUser = createdUser;
this.createdTime = createdTime;
this.modifiedUser = modifiedUser;
this.modifiedTime = modifiedTime;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getUuid() {
return uuid;
} public void setUuid(String uuid) {
this.uuid = uuid;
} public String getCreatedUser() {
return createdUser;
} public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
} public Date getCreatedTime() {
return createdTime;
} public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
} public String getModifiedUser() {
return modifiedUser;
} public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser;
} public Date getModifiedTime() {
return modifiedTime;
} public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", gender=" + gender
+ ", phone=" + phone + ", email=" + email + ", uuid=" + uuid + ", createdUser=" + createdUser
+ ", createdTime=" + createdTime + ", modifiedUser=" + modifiedUser + ", modifiedTime=" + modifiedTime
+ "]";
} }

创建包和接口

创建`com.demo.dao.UserMapper`接口,并添加抽象方法:

 package com.demo.dao;

 import com.demo.pojo.User;

 public interface UserMapper {
/**
* 添加用户信息
* @param user 用户信息
* @return 返回有效行数
*/
Integer insert(User user);
}

##  配置XML映射

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- namespace:匹配的接口 -->
<mapper namespace="com.demo.dao.UserMapper"> <!-- 添加用户信息 -->
<!-- Integer insert(User user) --> <insert id="insert" parameterType="com.demo.pojo.User"
useGeneratedKeys="true" keyProperty="id">
INSERT INTO
t_user (
username,
password,
gender,
phone,
email,
uuid,
created_user,
created_time,
modified_user,
modified_time
) VALUES (
#{username},
#{password},
#{gender},
#{phone},
#{email},
#{uuid},
#{createdUser},
#{createdTime},
#{modifiedUser},
#{modifiedTime}
)
</insert>
</mapper>

demo_1的更多相关文章

  1. seajs的使用

    写在前面 seajs是什么? Seajs是一个js文件加载器. 遵循 CMD 规范模块化开发,依赖的自动加载.配置的简洁清晰. 用于Web开发的模块加载工具,提供简单.极致的模块化体验 一:使用 文件 ...

  2. Mac下安装ionic和cordova,并生成iOS项目

    为了开发HTML5,除了最新使用React Native等之外,目前首选的为稳定的ionic+Angularjs来开发iOS和android. Ionic(ionicframework一款接近原生的H ...

  3. MAC OSX环境下cordova+Ionic的安装配置

    一.简介 1.Ionic是什么 IONIC 是目前最有潜力的一款 HTML5 手机应用开发框架.通过 SASS 构建应用程序,它提供了很多 UI 组件来帮助开发者开发强大的应用. 它使用 JavaSc ...

  4. 类Collections的静态方法的使用(代码)

    package cn.itcast.p2.toolclass.collections.demo; import java.util.ArrayList; import java.util.Collec ...

  5. java-API中的常用类,新特性之-泛型,高级For循环,可变参数

    API中的常用类 System类System类包含一些有用的类字段和方法.它不能被实例化.属性和方法都是静态的. out,标准输出,默认打印在控制台上.通过和PrintStream打印流中的方法组合构 ...

  6. Mysql存储过程语法

    一口气弄完了! 一.条件语句if-then-else: create procedure demo_1(in param int) begin declare var int; ; then inse ...

  7. 我的Github之旅(一)

    第一站:本地环境中的Github配置 1.参考链接 作为初学者,需要了解的有[本地环境中的github配置(基于mac)][1],以及git知识,这里推荐一个网站[猴子都能懂的Git入门][2],最后 ...

  8. canvas 2d 贴图技术实践

    最近在公司内部的技术协会论坛里闲逛的时候,无意中发现了一篇手淘前端大牛岑安两年前写的博文,讲述了canvas的2d贴图技术.看到后觉得相当神奇.于是就自己实现了一下.不过岑安前辈的那篇博文也只是大概讲 ...

  9. 【转】AspNetPager分页控件用法

    AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码: 1.首先到www.w ...

随机推荐

  1. Android进程间通信IPC

    一.IPC的说明 IPC是Inter-Process Communication的缩写,含义为进程间通信或跨进程通信,是指两个进程之间进行数据交换的过程. IPC不是Android独有的,任何一个操作 ...

  2. django介绍及路由系统

    第一:Python的web框架介绍 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引 ...

  3. VIM常用快捷键(转载)

    移动光标 h,j,k,l 上,下,左,右 ctrl-e 移动页面 ctrl-f 上翻一页 ctrl-b 下翻一页 ctrl-u 上翻半页 ctrl-d 下翻半页 w 跳到下一个字首,按标点或单词分割 ...

  4. Best Cow Line---POJ 3617(贪心)

    FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competiti ...

  5. c# ef 排序字段动态,构建动态Lambda和扩展方法OrderBy

    1.动态构建排序 Lambda /// <summary> /// 获取排序Lambda(如果动态排序,类型不同会导致转换失败) /// </summary> /// < ...

  6. 轻量级C#网络通信组件StriveEngine —— C/S通信开源demo(附源码)

    前段时间,有几个研究ESFramework网络通讯框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送.不需要P2P.不存在好 ...

  7. TCP协议学习总结(中)

    很多人都说TCP协议是一个十分复杂的协议,在学习当中,我对协议每一个问题都分解学习后,每一个分解我都能体会和理解它的要点,并不难理解.但我把这些拆分的细节合并后,确认感觉这样一个协议相对“臃肿”但又好 ...

  8. .NET Core 微服务架构 Steeltoe 使用(基于 Spring Cloud)

    阅读目录: 1. Spring Cloud Eureka 注册服务及调用 2. Spring Cloud Hystrix 断路器 3. Spring Cloud Hystrix 指标监控 4. Spr ...

  9. Java两种方法实现循环报数

    问题描述: 十个猴子围成一圈选大王,依次1-3 循环报数,报到3 的猴子被淘汰,直到最后一只猴子成为大王.问,哪只猴子最后能成为大王? 方法一:Java链表 public class TestAll ...

  10. [Swift]LeetCode7. 反转整数 | Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...