1 创建maven工程
不带骨架的java工程
2.创建子模块
itcast-ssm-web
itcast-ssm-domain
itcast-ssm-service
itcast-ssm-dao
itcast-ssm-utils
其中创建itcast-ssm-web时注意我们选择一个web工程
3.在父工程下的pom.xml添加依赖
<?xml version="1.0" encoding="UTF-8"?> <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>com.itheima.heima_ssm</groupId> <artifactId>heima_ssm</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <properties> <spring.version>5.0.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <mybatis.version>3.4.5</mybatis.version> <spring.security.version>5.0.1.RELEASE</spring.security.version> </properties> <modules> <module>heima_ssm_dao</module> <module>heima_ssm_service</module> <module>heima_ssm_domain</module> <module>heima_ssm_utils</module> <module>heima_ssm_web</module> </modules> <dependencies> <!-- spring --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> </dependencies> <!--插件--> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
4.在itcast-ssm-web下的main目录下面创建java和resources两个包,在resourcesl里面编写整合ssm配置文件
5.配置文件整合applicationContext.xml,spring-mvc.xml,web.xml
配置文件里面的内容就在这个图中applicationcontext.xml是spring的配置文件
spring整合mybatis就是用spring来控制SqlSessionFactory的管理
6.编写domain,dao,service,和controller里面的代码,例如查询所有
domain中的product类
package com.domain; import com.utils.DateUtils; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * 产品信息 */ public class Product { private String id; // 主键 private String productNum; // 编号 唯一 private String productName; // 名称 private String cityName; // 出发城市 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm") private Date departureTime; // 出发时间 // 这个是为了格式化数据 方便展示 private String departureTimeStr; private double productPrice; // 产品价格 private String productDesc; // 产品描述 private Integer productStatus; // 状态 0 关闭 1 开启 // 这个也是为了格式化数据,方便展示 private String productStatusStr; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getProductNum() { return productNum; } public void setProductNum(String productNum) { this.productNum = productNum; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public Date getDepartureTime() { return departureTime; } public void setDepartureTime(Date departureTime) { this.departureTime = departureTime; } public String getDepartureTimeStr() { if(departureTime!=null){ departureTimeStr= DateUtils.date2String(departureTime,"yyyy-MM-dd HH:mm:ss"); } return departureTimeStr; } public void setDepartureTimeStr(String departureTimeStr) { this.departureTimeStr = departureTimeStr; } public double getProductPrice() { return productPrice; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } public String getProductDesc() { return productDesc; } public void setProductDesc(String productDesc) { this.productDesc = productDesc; } public Integer getProductStatus() { return productStatus; } public void setProductStatus(Integer productStatus) { this.productStatus = productStatus; } public String getProductStatusStr() { // 显示状态信息 if (productStatus != null) { // 状态 0 关闭 1 开启 if(productStatus==0) productStatusStr="关闭"; if(productStatus==1) productStatusStr="开启"; } return productStatusStr; } public void setProductStatusStr(String productStatusStr) { this.productStatusStr = productStatusStr; } }
dao里面的
package com.dao; import com.domain.Product; import org.apache.ibatis.annotations.Select; import java.util.List; public interface IProductDao { // 查询所有 @Select("select * from product") List<Product> findAll() throws Exception; // @Insert("insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) // values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})") // void save(Product product); }
service
package com.service; import com.domain.Product; import java.util.List; public interface IProductService { public List<Product> findAll() throws Exception; } 实现类 package com.service.impl; import com.domain.Product; import com.dao.IProductDao; import com.service.IProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service //事务 @Transactional public class ProductServiceImpl implements IProductService { //自动按照类型注入 @Autowired private IProductDao productDao; public List<Product> findAll() throws Exception { // List<Product> list = productDao.findAll(); return productDao.findAll(); } }
controller
package com.controller; import com.domain.Product; import com.service.IProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; //控制器 @Controller @RequestMapping("/product") public class ProductController { // 引入serviced @Autowired private IProductService productService; //查询全部产品 @RequestMapping("/findAll.do") public ModelAndView findAll() throws Exception { //创建一个ModelAndView对象 ModelAndView mv = new ModelAndView(); List<Product> products = productService.findAll(); mv.addObject("productList", products); mv.setViewName("product-list"); // mv.setViewName("test"); return mv; } }