ssm作为我们java程序猿的开发神器,极大地简化了开发,为我们提供了很大的便利,今天聊一聊ssm的整合。
这是我的架构图,
看完了了架构图那么就开始架构搭建:
**//配置文件头信息**
Archetype Created Web Application
**//定义filter过滤器,防止中文乱码**
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
//filter-mapping映射,设置过滤所有路径
CharacterEncodingFilter
/*
**// 配置mvc DispatcherServlet 并加载配置文件**
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
DispatcherServlet
/
**//配置监听器,加载spring-service配置文件**
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-service.xml
注意:复制时需要把上面的**中的文字去掉,只是为了更容易辨认,并不是xml注释
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
jdbc.driver:
注册驱动
jdbc.url:
数据库地址
jdbc.username:
数据库用户名需要改成你自己的数据库用户名。
jdbc.password:
数据库密码需要改成你自己的数据库密码。
package com.huawei.controller;
import com.huawei.domain.Account;
import com.huawei.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping(value = "/account",produces = "text/html;charset=utf-8")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 新增
* @param account
* @return
*/
@RequestMapping("/save")
@ResponseBody
public String save(Account account){
accountService.add(account);
return "保存成功";
}
/**
* 查询所有
* @return
*/
@RequestMapping("/findAll")
@ResponseBody //保证不跳转页面
public List<Account> findAll(){
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println(account);
}
return accountList;
}
}
@Controller
将本类创建为实例,并放到IOC
容器中,还有@Component
,@Service
,@Repository
作用都一致。只是为了见名知意Controller层采用了@Controller
,Service层采用了@Service
,Dao层采用了@Repository
。@Component
作为其他类实例创建用。
@RequestMapping(value = "/account",produces = "text/html;charset=utf-8")
,springmvc提供的注解,用于映射地址。DispatcherServlet就是通过这个注解中的地址,来找到这个方法,访问并执行的。第一个参数:配置的方法的路径,第二个参数:配置的防止中文乱码。
@ResponseBody
保证返回数据不跳转页面,同时保证返回数据为json格式。
@Autowired
将创建的实体类从IOC
注入到本类。
package com.huawei.service;
import com.huawei.domain.Account;
import java.util.List;
public interface AccountService {
List<Account> findAll();
void add(Account account);
}
package com.huawei.service.impl;
import com.huawei.Mapper.AccountMapper;
import com.huawei.domain.Account;
import com.huawei.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public List<Account> findAll() {
return accountMapper.findAll();
}
@Override
public void add(Account account) {
accountMapper.add(account);
}
}
@Service
上面已做讲解,service层主要用来实现业务逻辑。
import java.util.List;
@Repository
public interface AccountMapper {
@Select("select * from account")
List<Account> findAll();
@Insert("insert into account values (#{id},#{name},#{money})")
void add(Account account);
}
package com.huawei.domain;
public class Account {
private int id;
private String name;
private double money;
public Account() {
}
public Account(int id, String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
pom文件
<?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</groupId>
<artifactId>itheima_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>itheima_ssm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<!--commons-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--servlet和jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
<version>2.0</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
好了今天的ssm整合就说到这了。