SSM17.1【SSM框架整合--搭建初始环境】
SSM框架整合思路
1、创建数据库表结构
1 create table account( 2 id int primary key auto_increment, 3 name varchar(20), 4 money double 5 );
2、创建maven工程(利用webapp骨架模板)
3、在maven配置文件pom.xml中导入依赖坐标
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>SSM17</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>SSM17 Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 17 <!--maven使用properties指定版本号--> <!--版本锁定--> 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <maven.compiler.source>1.8</maven.compiler.source> 21 <maven.compiler.target>1.8</maven.compiler.target> 22 23 <spring.version>5.0.2.RELEASE</spring.version> 24 <slf4j.version>1.6.6</slf4j.version> 25 <log4j.version>1.2.12</log4j.version> 26 <mysql.version>5.1.6</mysql.version> 27 <mybatis.version>3.4.5</mybatis.version> 28 </properties> 29 30 31 <dependencies> 32 <!-- spring --> 33 <dependency> 34 <groupId>org.aspectj</groupId> 35 <artifactId>aspectjweaver</artifactId> 36 <version>1.6.8</version> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-aop</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-context</artifactId> 48 <version>${spring.version}</version> 49 </dependency> 50 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-web</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-webmvc</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-test</artifactId> 66 <version>${spring.version}</version> 67 </dependency> 68 69 <dependency> 70 <groupId>org.springframework</groupId> 71 <artifactId>spring-tx</artifactId> 72 <version>${spring.version}</version> 73 </dependency> 74 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-jdbc</artifactId> 78 <version>${spring.version}</version> 79 </dependency> 80 81 <dependency> 82 <groupId>junit</groupId> 83 <artifactId>junit</artifactId> 84 <version>4.12</version> 85 <scope>test</scope> 86 </dependency> 87 88 <dependency> 89 <groupId>mysql</groupId> 90 <artifactId>mysql-connector-java</artifactId> 91 <version>${mysql.version}</version> 92 </dependency> 93 94 <dependency> 95 <groupId>javax.servlet</groupId> 96 <artifactId>servlet-api</artifactId> 97 <version>2.5</version> 98 <scope>provided</scope> 99 </dependency> 100 101 <dependency> 102 <groupId>javax.servlet.jsp</groupId> 103 <artifactId>jsp-api</artifactId> 104 <version>2.0</version> 105 <scope>provided</scope> 106 </dependency> 107 108 <dependency> 109 <groupId>jstl</groupId> 110 <artifactId>jstl</artifactId> 111 <version>1.2</version> 112 </dependency> 113 114 <!-- log start --> 115 <dependency> 116 <groupId>log4j</groupId> 117 <artifactId>log4j</artifactId> 118 <version>${log4j.version}</version> 119 </dependency> 120 121 <dependency> 122 <groupId>org.slf4j</groupId> 123 <artifactId>slf4j-api</artifactId> 124 <version>${slf4j.version}</version> 125 </dependency> 126 127 <dependency> 128 <groupId>org.slf4j</groupId> 129 <artifactId>slf4j-log4j12</artifactId> 130 <version>${slf4j.version}</version> 131 </dependency> 132 <!-- log end --> 133 134 <dependency> 135 <groupId>org.mybatis</groupId> 136 <artifactId>mybatis</artifactId> 137 <version>${mybatis.version}</version> 138 </dependency> 139 140 <!--mybatis-spring--> 141 <dependency> 142 <groupId>org.mybatis</groupId> 143 <artifactId>mybatis-spring</artifactId> 144 <version>1.3.0</version> 145 </dependency> 146 147 <dependency> 148 <groupId>c3p0</groupId> 149 <artifactId>c3p0</artifactId> 150 <version>0.9.1.2</version> 151 <type>jar</type> 152 <scope>compile</scope> 153 </dependency> 154 </dependencies> 155 156 157 <build> 158 <finalName>SSM17</finalName> 159 <pluginManagement> 160 <plugins> 161 <plugin> 162 <groupId>org.apache.maven.plugins</groupId> 163 <artifactId>maven-compiler-plugin</artifactId> 164 <version>3.2</version> 165 <configuration> 166 <source>1.8</source> 167 <target>1.8</target> 168 <encoding>UTF-8</encoding> 169 <showWarnings>true</showWarnings> 170 </configuration> 171 </plugin> 172 </plugins> 173 </pluginManagement> 174 </build> 175 176 177 </project>
4、补齐目录
5、将该项目部署到tomcat服务器
6、创建包结构
7、编写实体类
1 package com.haifei.domain; 2 3 import java.io.Serializable; 4 5 /** 6 * 实体类-账户 7 */ 8 public class Account implements Serializable { 9 10 /* 11 为啥不用基本数据类型而采用对应的包装类? 12 https://www.cnblogs.com/vincentmax/p/14285762.html 13 14 在数据库中,数据的默认值是 null,而在 Java 中,基本类型的默认值不是 null 15 int 类型的默认值是 0,而包装类Integer的默认值是 null,因为是对象 16 */ 17 private Integer id; 18 private String name; 19 private Double money; 20 21 @Override 22 public String toString() { 23 return "Account{" + 24 "id=" + id + 25 ", name='" + name + '\'' + 26 ", money=" + money + 27 '}'; 28 } 29 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getName() { 39 return name; 40 } 41 42 public void setName(String name) { 43 this.name = name; 44 } 45 46 public Double getMoney() { 47 return money; 48 } 49 50 public void setMoney(Double money) { 51 this.money = money; 52 } 53 }
8、编写dao接口
1 package com.haifei.dao; 2 3 import com.haifei.domain.Account; 4 5 import java.util.List; 6 7 /** 8 * dao接口-账户 9 */ 10 public interface AccountDao { 11 12 //查询所有账户 13 public List<Account> findAll(); 14 15 //保存账户信息 16 public void saveAccount(Account account); 17 18 }
9、编写service接口及其实现类
1 package com.haifei.service; 2 3 import com.haifei.domain.Account; 4 5 import java.util.List; 6 7 /** 8 * service接口-账户 9 */ 10 public interface AccountService { 11 12 //查询所有账户 13 public List<Account> findAll(); 14 15 //保存账户信息 16 public void saveAccount(Account account); 17 18 }
1 package com.haifei.service.impl; 2 3 import com.haifei.domain.Account; 4 import com.haifei.service.AccountService; 5 6 import java.util.List; 7 8 /** 9 * service接口实现类-账户 10 */ 11 public class AccountServiceImpl implements AccountService { 12 13 @Override 14 public List<Account> findAll() { 15 System.out.println("业务层:查询所有账户"); 16 return null; 17 } 18 19 @Override 20 public void saveAccount(Account account) { 21 System.out.println("业务层:保存账户信息"); 22 } 23 24 }
10.编写web层controller类
1 package com.haifei.controller; 2 3 /** 4 * web层-账户 5 */ 6 public class AccountController { 7 8 }