IDEA /Maven /mysql8.0 ;
首先创建maven工程:
工程目录如下:
1.配置pom.xml; 加上<packaging>war</packaging>,不然后面会无法添加ecploded工程;
<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>
<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>
</properties>
<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-web</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!-- 数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</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-->
<!-- mybatis -->
<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>
</dependencies>
<build>
<finalName>ssm02</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
2. 在 resource 文件夹下,创建web.xml;
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数,创建完DispatcherServlet对象后,就加载spring-mvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--服务器启动的时候,就创建DispatcherServlet对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置spirng的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--设置spring核心配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
3.在 resource 文件夹下,创建 applicationContext.xml 文件,(这里放在了一起)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--配置spring要扫描的包,其中不包括controller-->
<context:component-scan base-package="com.xxx">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--spring整合mybatis框架-->
<!--配置连接池-->
<bean id="mySql" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm01?serverTimezone=Asia/Shanghai&characterEncoding=UTF8"/>
<property name="user" value="root"/>
<property name="password" value="abc123"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mySql"/>
</bean>
<!--配置AccountDao接口所在的包-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"></property>
</bean>
</beans>
4. 在 resource 文件夹下,创建log4j ;选择这个创建,可以直接生成后缀;
log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%-5p - %m%n
5. 创建实体类 Account ;如果pom中加坐标,也可以用注解;
package com.xxx.domain;
public class Account {
private int id;
private String name;
private double 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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
6. 创建 AccountController 类,这里可以加入输出语句,通过输出控制台来测试;
package com.xxx.controller;
import com.xxx.domain.Account;
import com.xxx.serice.impl.AccountServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
@RequestMapping("/accounts")
public class AccountController {
//注入service的实现类
@Autowired
private AccountServiceImp accountServiceImp;
//查询所有账户
@RequestMapping("/findAll")
public String findAll(Model model){
System.out.println("web层,查询所有账户");
List<Account> accounts = accountServiceImp.findAll();
model.addAttribute("accounts",accounts);
return "allcounts";
}
//根据id查询账户
@RequestMapping("/findByid")
public String findByid(Integer id,Model model){
System.out.println("web层,根据id查询账户");
Account account = accountServiceImp.findById(id);
model.addAttribute("account",account);
//测试是否进入控制器
System.out.println(id);
return "account";
}
//增加账户
@RequestMapping("/insertAccount")
public void insertAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("web层,增加账户");
accountServiceImp.insertAccount(account);
response.sendRedirect(request.getContextPath()+"/accounts/findAll");
}
//删除账户
@RequestMapping("/deleteAccount")
public void deleteAccount(Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("web层,删除账户");
accountServiceImp.deleteAccount(id);
response.sendRedirect(request.getContextPath()+"/accounts/findAll");
}
//修改账户
@RequestMapping("/updateAccount")
public void updateAccount(Account account,HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("web层,修改账户");
accountServiceImp.updateAccount(account);
response.sendRedirect(request.getContextPath()+"/accounts/findAll");
}
}
7. 创建 IAccountService 接口;
package com.xxx.serice;
import com.xxx.domain.Account;
import java.util.List;
public interface IAccountService {
//查询所有账户
List<Account> findAll();
//根据id查询账户
Account findById(Integer id);
//增加账户
void insertAccount(Account account);
//删除账户
void deleteAccount(Integer id);
//修改账户信息
void updateAccount(Account account);
}
8. 创建 IAccountDao 接口;
package com.xxx.dao;
import com.xxx.domain.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface IAccountDao {
//查询所有账户
@Select("select * from account ")
List<Account> findAll();
//根据id查询账户
@Select("select * from account where id = #{id}")
Account findById(int id);
//增加账户
@Insert("insert into account (name,money) values (#{name},#{money}) ")
void insertAccount(Account account);
//删除账户
@Delete("delete from account where id = #{id}")
void deleteAccount(Integer id);
//修改账户信息
@Update("update account set name = #{name},money=#{money} where id = #{id}")
void updateAccount(Account account);
}
9. 在serice下创建 impl 包,包下创建 AccountServiceImp 类;
package com.xxx.serice.impl;
import com.xxx.dao.IAccountDao;
import com.xxx.domain.Account;
import com.xxx.serice.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Service
@RequestMapping("/counts")
public class AccountServiceImp implements IAccountService {
@Autowired
private IAccountDao iAccountDao;
@Override
public List<Account> findAll() {
System.out.println("service层,查询所有账户");
return iAccountDao.findAll();
}
@Override
public Account findById(Integer id) {
System.out.println("service层,根据id查询账户");
System.out.println(id);
return iAccountDao.findById(id);
}
@Override
public void insertAccount(Account account) {
System.out.println("service层,增加账户");
iAccountDao.insertAccount(account);
System.out.println(account);
}
@Override
public void deleteAccount(Integer id) {
System.out.println("service层,删除账户");
iAccountDao.deleteAccount(id);
System.out.println(id);
}
@Override
public void updateAccount(Account account) {
System.out.println("service层,更新账户");
iAccountDao.updateAccount(account);
System.out.println(account);
}
}
10. 创建数据库:
运行结果如下:
至此框架搭建成功,
由于不是很理解,文件顺序不是很清晰;上面是最终的形式,按这个顺序会有飘红提示;
遇到的问题:
1. IDEA连接数据库8.0会有时区问题;
2. 配置Tomcat时,需要加入 导入apache-tomcat\apache-tomcat-8.5.82\webapps\ROOT文件 ;
3. 由于是复制粘贴,dispatcherServlet 会飘红,需要刷新一下,如图;