IntelliJ IDEA +Maven + Spring+SpringMVC+Mybatis项目整合环境要求
本帖转自微信公众号:猿梦
链接为:https://mp.weixin.qq.com/s?__biz=MzAwODY2NDE0Nw==&mid=2247483714&idx=1&sn=44bb479546f0e7670614aa50078d5527&chksm=9b6a247eac1dad686cb249f4b2f19c85cf2ddac8e613c2b2bfbbc52e4724f9d5659a58496bad&scene=126&sessionid=1600571365&key=e559d02183b43ff864e88d11d080b8c85391eb4d1c967121b545178b4835b736245e913de25e5dfa0e4e62e60941dc0b30e83d233ba4c23ce7443cd927dac59430bd3fadf412e57b165e9c6e61afc2a3c235ec2580b731336cd6aed498024bb335bc94d48b59ac974b34ef0e2160aa9317ab56d05386386977431dcb84c37b15&ascene=1&uin=MTg2NjI1MDA3Mw%3D%3D&devicetype=Windows+10+x64&version=62090529&lang=zh_CN&exportkey=Aa%2FCvsdSSO5x%2FBv7Ngr4N9I%3D&pass_ticket=tlN84rmMO5Wq4E4deUzFkFJBsH%2Fm5Gv5JWihspXAZ6Ov10CK4e7R%2BbYovPh6yZmn&wx_header=0
环境要求
IDEA
MYSQL 5.5
Tomcat 7
MAVEM 3.6.1
数据库环境
CREATE DATABASE ssm;
USE ssm;
CREATE TABLE book(
id varchar(50) NOT NULL COMMENT '书id',
book_name varchar(100) NOT NULL COMMENT '书名',
author varchar(30) NOT NULL COMMENT '作者',
images varchar(255) NOT NULL COMMENT '图片',
create_time datetime NOT NULL COMMENT '出品日期',
price double NOT NULL COMMENT '价钱');
项目创建
创建maven项目
添加文件夹
修改web.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.meng</groupId>
<artifactId>ssm-merge</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm-merge 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.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--1、依赖:数据库驱动,连接池,servlet,jsp,el
mybatis mybatis-spring,spring 织入
lombok,json,ommons-fileupload 日志-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<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>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</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-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--2、静态资源到处问题-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
启动tomcat
访问服务器
创建项目结构
Mybatis层
创建Mybatis配置文件
<?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>
<!--添加设置-->
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
<!--日志使用logj,需要导入依赖以及log4j的属性文件-->
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--实体类音色别名-->
<typeAliases>
<package name="com.meng.merge.bean"/>
</typeAliases>
</configuration>
创建实体类
package com.meng.merge.bean;
import lombok.Data;
import lombok.Setter;
import java.util.Date;
@Data
public class Book {
private String id;
private String bookName;
private String author;
private Date createTime;
private String images;
private Double price;
}
创建Mapper接口
package com.meng.merge.dao;
import com.meng.merge.bean.Book;
import java.util.List;
import java.util.Map;
public interface BookMapper {
//添加一本书
int addBook();
//更新一本书
int updateBookById(Map<String, Object> map);
//删除书
int deleteBookById(int id);
//批量删除书
int batchDeleteBooks(int[] ids);
//查询
List<Book> selectBooks(Map<String, Object> map);
//统计
int getCounts(Map<String, Object> map);
}
创建Mapper映射文件
<?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.meng.merge.dao.BookMapper">
<insert id="addBook" parameterType="book">
insert into book values(#{id},#{bookName},#{author},#{images},#{createTime},#{price})
</insert>
<update id="updateBookById" parameterType="int">
update book
<set>
<if test="null != bookName and bookName != ''">
book_name = #{bookName}
</if>
<if test="null != author and author != ''">
author = #{author}
</if>
<if test="null != createTime and createTime != ''">
create_time = #{createTime}
</if>
<if test="null != images and images != ''">
images = #{images}
</if>
<if test="null != price and price != ''">
price = #{price}
</if>
</set>
</update>
<delete id="deleteBookById" parameterType="int">
delete from book where id = #{id}
</delete>
<delete id="batchDeleteBooks" parameterType="collection">
delete from book
<where>
<foreach collection="array" item="id" separator="or" open="(" close=")">
id = #{id}
</foreach>
</where>
</delete>
<select id="selectBooks" resultType="book" parameterType="map">
select * from book
<where>
<if test="null != bookName and bookName != ''">
book_name like '#{bookName}'
</if>
<if test="null != author and author != ''">
and author like '#{author}'
</if>
<if test="null != createTime and createTime != ''">
and create_time = like '#{createTime}'
</if>
<if test="null != price and price != ''">
and price = like '#{price}'
</if>
</where>
order by price asc limit #{pageSize},#{limit}
</select>
<select id="getCounts" resultType="int" parameterType="map">
select count(*) from book
<where>
<if test="null != bookName and bookName != ''">
book_name like '#{bookName}'
</if>
<if test="null != author and author != ''">
and author like '#{author}'
</if>
<if test="null != createTime and createTime != ''">
and create_time = like '#{createTime}'
</if>
<if test="null != price and price != ''">
and price = like '#{price}'
</if>
</where>
</select>
</mapper>
创建业务层
BookService.java
package com.meng.merge.service;
import com.meng.merge.bean.Book;
import java.util.List;
import java.util.Map;
public interface BookService {
//添加一本书
int addBook(Book book);
//更新一本书
int updateBookById(Map<String, Object> map);
//删除书
int deleteBookById(int id);
//批量删除书
int batchDeleteBooks(int[] ids);
//查询
List<Book> selectBooks(Map<String, Object> map);
//统计
int getCounts(Map<String, Object> map);
}
BookServiceImpl.java
package com.meng.merge.service.impl;
import com.meng.merge.bean.Book;
import com.meng.merge.dao.BookMapper;
import com.meng.merge.service.BookService;
import java.util.List;
import java.util.Map;
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
@Override
public int addBook(Book book) {
return bookMapper.addBook(book);
}
@Override
public int updateBookById(Map<String, Object> map) {
return bookMapper.updateBookById(map);
}
@Override
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
@Override
public int batchDeleteBooks(int[] ids) {
return bookMapper.batchDeleteBooks(ids);
}
@Override
public List<Book> selectBooks(Map<String, Object> map) {
return bookMapper.selectBooks(map);
}
@Override
public int getCounts(Map<String, Object> map) {
return bookMapper.getCounts(map);
}
}
Spring层
Spring整合层dao
spring-dao.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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--1、关联数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--2、配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3、sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--4、当定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--5、配置mapper文件映射-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置包扫描,动态实现dao接口可以注册到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.meng.merge.dao"/>
</bean>
</beans>
Spring整合层serivce
spring-service.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--1、扫描包-->
<context:component-scan base-package="com.meng.merge.service"/>
<!--2、声明事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--aop事务支持-->
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 传播行为 -->
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.meng.merge.service.*.*(..))" />
</aop:config>
</beans>
添加注解
@Service
@Autowired
@Repository
SpringMVC层
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加载Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载SpringMVC-->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--中文乱码过滤器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
创建springmvc配置文件
<?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">
<!--1、包扫描-->
<context:component-scan base-package="com.meng.merge.controller"/>
<!--2、注解驱动-->
<mvc:annotation-driven/>
<!--3、静态资源过滤-->
<mvc:default-servlet-handler/>
<!--4、视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
测试
创建contrller
package com.meng.merge.controller;
import com.meng.merge.service.BookService;
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 java.util.HashMap;
import java.util.Map;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/book/list")
public String getBooks(Model model,int pageSize,int limit){
Map<String,Object> map = new HashMap<>();
map.put("pageSize",(pageSize-1)*limit);
map.put("limit",limit);
model.addAttribute("list",bookService.selectBooks(map));
return "list";
}
}
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${list}" var="book">
${book.bookName}-${book.author}-${book.createTime}-${book.price}<br/>
</c:forEach>
</body>
</html>
转载自:https://mp.weixin.qq.com/s?__biz=MzAwODY2NDE0Nw==&mid=2247483714&idx=1&sn=44bb479546f0e7670614aa50078d5527&chksm=9b6a247eac1dad686cb249f4b2f19c85cf2ddac8e613c2b2bfbbc52e4724f9d5659a58496bad&scene=126&sessionid=1600571365&key=e559d02183b43ff864e88d11d080b8c85391eb4d1c967121b545178b4835b736245e913de25e5dfa0e4e62e60941dc0b30e83d233ba4c23ce7443cd927dac59430bd3fadf412e57b165e9c6e61afc2a3c235ec2580b731336cd6aed498024bb335bc94d48b59ac974b34ef0e2160aa9317ab56d05386386977431dcb84c37b15&ascene=1&uin=MTg2NjI1MDA3Mw%3D%3D&devicetype=Windows+10+x64&version=62090529&lang=zh_CN&exportkey=Aa%2FCvsdSSO5x%2FBv7Ngr4N9I%3D&pass_ticket=tlN84rmMO5Wq4E4deUzFkFJBsH%2Fm5Gv5JWihspXAZ6Ov10CK4e7R%2BbYovPh6yZmn&wx_header=0