我练习的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. windows系统dos窗口全屏

    第一次进入博客园 2017年12月7日 之前使用dos窗口时都输入的是简短的指令,今天突然感觉小框看着不舒服,就找了一下度娘,在这里感谢万能的百度,一鞠躬. 1.win+r打开dos命令窗口 2.cm ...

  2. vue简单指令笔记

    v-once 执行一次性插值,数据改变插值处内容不会更新 <span v-once>这个将不会改变: {{ msg }}</span> v-text 插入文本 <!--两 ...

  3. 2018申请淘宝客AppKey

    1.www.alimama.com 申请账号进入后2.进入我的联盟,按下面的步骤 完成以后等待网站审核. 3.审核完成后 按以下的图,申请进入开放平台或得appkey 4.最后就可以进入开放平台申请看 ...

  4. libguestfs手册(1): 架构

    要编辑一个image,则运行下面的命令 guestfish -a ubuntutest.img ><fs> 会弹出一个命令行工具 运行run ><fs> run 我 ...

  5. 简述一下MVC和MVVM

    一. MVC 我们先来了解一下什么是MVC. MVC:分别所指Model.View.Controller. MVC为标准的设计模式,是官方推荐的权威的规范模式. 视图(View):用户交互界面. 控制 ...

  6. monkey 命令详解

    monkey命令详解   1.  $ adb shell monkey <event-count>                <event-count>是随机发送事件数 例 ...

  7. Linux之相关英文缩写含义

    1.目录名: 名称 英文 英文含义 描述 /boot boot 引导 操作系统的内核及在引导过程中使用的文件 /root root 根 系统管理员的主目录(根目录) /run run 运行 系统运行时 ...

  8. [Abp 源码分析]六、工作单元的实现

    0.简介 在 Abp 框架内部实现了工作单元,在这里讲解一下,什么是工作单元? Unit Of Work(工作单元)模式用来维护一个由已经被业务事物修改(增加.删除或更新)的业务对象组成的列表.Uni ...

  9. Python 创建递归文件夹

    # 创建递归文件夹 def createfiles(filepathname): try: os.makedirs(filepathname) except Exception as err: pri ...

  10. BBS论坛(二十一)

    21.1.编辑轮播图功能完成 (1)cms_banners.html 把属性绑定到<tr>上面,方便找到各属性的值 <tbody> {% for banner in banne ...