1. The SSM framework integration directory
The basic framework of the SSM is as follows:
2. Database table Account
CREATE DATABASE ssm;
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(100),
money DOUBLE(7.2));INSERT INTO account VALUES(1,"KangK",5000), (2,"Jane",5000);
Copy the code
3. java
3.1 the Account
public class Account {
private int id;
private String name;
private double money;
@Override
public String toString(a) {
return "Account{" +
"id=" + id +
", name='" + name + '\' ' +
", money=" + money +
'} ';
}
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney(a) {
return money;
}
public void setMoney(double money) {
this.money = money; }}Copy the code
3.2 AccountMapper
public interface AccountMapper {
List<Account> selectAll(a);
void saveAccount(Account account);
}
Copy the code
3.3 AccountService
public interface AccountService {
List<Account> findAll(a);
void save(Account account);
}
Copy the code
3.4 AccountServiceImpl
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public List<Account> findAll(a) {
return accountMapper.selectAll();
}
@Override
public void save(Account account) { accountMapper.saveAccount(account); }}Copy the code
3.5 AccountController
@RequestMapping("/account")
@Controller
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping("/select")
@ResponseBody
public ModelAndView select(ModelAndView modelAndView) {
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println(account);
}
modelAndView.addObject("accountList", accountList);
modelAndView.setViewName("accountList");
return modelAndView;
}
@RequestMapping("/insert")
@ResponseBody
public void update(Account account) { accountService.save(account); }}Copy the code
4. resources
4.1 AccountMapper. XML
<! DOCTYPEmapper PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="one.mapper.AccountMapper">
<select id="selectAll" resultType="account">
select * from account;
</select>
<insert id="saveAccount" parameterType="account">
insert into account values(#{id}, #{name}, #{money});
</insert>
</mapper>
Copy the code
4.2 applicationContext – spring. XML
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<! -- 1. Component scan -->
<context:component-scan base-package="one.service"/>
<! -- import jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<! C3p0, please use DruidDataSource! -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<! -- configure MyBatis SqlSessionFactory! -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<! Create class for Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<! Accountmapper. Java class path -->
<property name="basePackage" value="one.mapper"/>
</bean>
<! -- configure transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="tx_Advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="tx_Advice" pointcut="execution(* one.service.*.*(..) )"/>
</aop:config>
</beans>
Copy the code
4.3 applicationContext – for springmvc. XML
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<! -- 1. Scan Controller -->
<context:component-scan base-package="one.controller"/>
<! -- 2. Configure the view parser -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<! -- 3. Annotation-driven -->
<mvc:annotation-driven/>
<! 4. Enable static resource access -->
<mvc:default-servlet-handler/>
</beans>
Copy the code
JDBC 4.4. The properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm? useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
Copy the code
4.5 the log4j properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=D:\\Temp\\log4j
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
Copy the code
4.6 SqlMapConfig. XML
MyBatis core configuration files are mostly replaced by Spring configuration files, which integrate MyBatis.
<! DOCTYPEconfiguration PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<! - alias - >
<typeAliases>
<typeAlias type="one.domain.Account" alias="account"/>
</typeAliases>
</configuration>
Copy the code
5. webapp
5.1 web. XML
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<! --1.Spring listener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<! -- 2. Configure front-end controller -->
<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:applicationContext-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<! -- 3. Garbled filter -->
<filter>
<filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
<url-pattern>/ * *</url-pattern>
</filter-mapping>
</web-app>
Copy the code
5.2 accountList. JSP
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> < HTML > <head> <title> Page </title> </head> <body> <h1> Display the account data list </h1> <table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>money</th>
</tr>
<c:forEach items="${accountList}" var="account">
<tr>
<td>${account.id}</td>
<td>${account.name}</td>
<td>${account.money}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Copy the code
5.3 save the JSP
<%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> title </title> </head> <body> <h1> Add the account information form </h1> <form name="accountForm" action="${pageContext.request.contextPath}/account/insert" method="post"> <label> Account name :<input type="text" name="name"></label><br> <label> Account amount :<input type="text" name="money"></label><br>
<input type="submit" value="Save"><br>
</form>
</body>
</html>
Copy the code
6. pom.xml
The basic dependencies obtained in THE SSM framework are as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>SSM_Latest</name>
<groupId>com.one</groupId>
<artifactId>SSM_Latest</artifactId>
<version>1.0 the SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</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.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.2</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-test</artifactId>
<version>5.0.5. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5. RELEASE</version>
</dependency>
</dependencies>
</project>
Copy the code
Original not easy 🧠reprint please indicate the source 🌊 if this article is helpful to you, like ao 🔥