目录
前言
1、创建maven工程
2、导入依赖坐标
pom.xml
3、编写数据库
4、编写实体类
Account
5、编写持久层接口
AccountMapper
6、编写业务层接口
AccountService
7、编写业务层实现类
AccountServiceImpl
8、编写 Controller
AccountController
9、编写配置文件
AccountMapper.xml
applicationContest.xml
jdbc.properties
log4j.properties
spring-mvc.xml
sqlMapConfig.xml
sqlMapConfig-spring.xml
10、编写web代码
web.xml
index.jsp
accountList.jsp
save.jsp
11、完成后代码框架
12、运行代码
前言
ssm整合分为以下步骤
1、创建项目
2、导入依赖坐标
3、编写数据库
4、编写服务代码
5、编写配置文件
6、编写前端代码
7、运行代码
1、创建maven工程
2、导入依赖坐标
pom.xml
<dependencies> <!-- spring相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.12.RELEASE</version> </dependency> <!-- servlet和jsp--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <!-- mybatis相关--> <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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
3、编写数据库
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for account -- ---------------------------- DROP TABLE IF EXISTS `account`; CREATE TABLE `account` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
4、编写实体类
Account
package com.ljx.domain; /** * @Author: Ljx * @Date: 2021/8/23 14:06 */ 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 + '}'; } }
5、编写持久层接口
AccountMapper
package com.ljx.mapper; import com.ljx.domain.Account; import java.util.List; /** * @Author: Ljx * @Date: 2021/8/23 14:08 */ public interface AccountMapper { void save(Account account); List<Account> findAll(); }
6、编写业务层接口
AccountService
package com.ljx.service; import com.ljx.domain.Account; import java.io.IOException; import java.util.List; /** * @Author: Ljx * @Date: 2021/8/23 14:10 */ public interface AccountService { void save(Account account) throws IOException; List<Account> findAll() throws IOException; }
7、编写业务层实现类
AccountServiceImpl
package com.ljx.service.impl; import com.ljx.domain.Account; import com.ljx.mapper.AccountMapper; import com.ljx.service.AccountService; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * @Author: Ljx * @Date: 2021/8/23 14:11 */ @Service("accountService") public class AccountServiceImpl implements AccountService { @Autowired private AccountMapper accountMapper; @Override public void save(Account account) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); AccountMapper mapper = sqlSession.getMapper(AccountMapper.class); mapper.save(account); sqlSession.commit(); sqlSession.close(); } @Override public List<Account> findAll() throws IOException { return accountMapper.findAll(); } }
8、编写 Controller
AccountController
package com.ljx.controller; import com.ljx.domain.Account; import com.ljx.service.AccountService; import com.ljx.service.impl.AccountServiceImpl; 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 org.springframework.web.servlet.ModelAndView; import java.io.IOException; import java.util.List; /** * @Author: Ljx * @Date: 2021/8/23 14:13 */ @Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; /** * 保存 * @param account * @return */ @RequestMapping(value = "/save", produces = "text/html;charset=UTF-8") @ResponseBody public String save(Account account) throws IOException { accountService.save(account); return "保存成功"; } /** * 查询 * @return */ @RequestMapping("/findAll") public ModelAndView findAll() throws IOException { List<Account> accountList = accountService.findAll(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("accountList",accountList); modelAndView.setViewName("accountList"); return modelAndView; } }
9、编写配置文件
AccountMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ljx.mapper.AccountMapper"> <insert id="save" parameterType="account"> insert into account values (#{id},#{name},#{money}) </insert> <select id="findAll" resultType="account"> select * from account </select> </mapper>
applicationContest.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解扫描--> <context:component-scan base-package="com.ljx"> <!-- 排除Controller扫描--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加载properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源信息--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置sessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis核心文件--> <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"/> </bean> <!-- 扫描mapper所在的包 为mapper创建实现类--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ljx.mapper"/> </bean> <!-- 声明式事务控制--> <!-- 平台事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务增强--> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事务的aop织入--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ljx.service.impl.*.*(..))"/> </aop:config> </beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.username= jdbc.password=
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=c:/mylog.log 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
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 组件扫描 主要扫描controller--> <context:component-scan base-package="com.ljx.controller"></context:component-scan> <!-- 配置mvc注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 内部资源视图解析器--> <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 开放静态资源的访问权限--> <mvc:default-servlet-handler/> </beans>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 加载properties文件--> <properties resource="jdbc.properties"></properties> <!-- 定义别名--> <typeAliases> <!-- <typeAlias type="com.ljx.domain.Account" alias="account"></typeAlias>--> <package name="com.ljx.domain"/> </typeAliases> <!-- 环境--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 加载映射--> <mappers> <!-- <mapper resource="com/ljx/mapper/AccountMapper.xml"/>--> <package name="com/ljx/mapper"/> </mappers> </configuration>
sqlMapConfig-spring.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 定义别名--> <typeAliases> <!-- <typeAlias type="com.ljx.domain.Account" alias="account"></typeAlias>--> <package name="com.ljx.domain"/> </typeAliases> </configuration>