1,创建maven项目:
然后,点击next
其次
2,在pom.xml导入相关依赖:(如果idea没有集成maven需要先集成maven)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<!--静态资源过滤:例如xml,html,图片-->
<resources>
<!-- mapper.xml文件在java目录下 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!-- mapper.xml文件在resources目录下-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<!-- 指定jdk编译版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- 此处设置了skip等于设置了把所有的测试编译都跳过,如果测试类写得有问题,也不会报错,所以一般不使用 -->
<!-- <skip>true</skip> -->
</configuration>
</plugin>
<!-- 指定资源文件的编码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
3,将项目变成web项目
4,创建需要的包
5,创建查询书籍测试各个测试类
Books实体类:
package com.demo.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
Controller控制类:
package com.demo.controller; import com.demo.bean.Books; import com.demo.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.List; @Controller @RequestMapping("/book") public class BookController { @Autowired private BookService bookService; @RequestMapping("/allBook") public String list(Model model) { List<Books> list = bookService.queryAllBook(); model.addAttribute("list", list); return "allBook"; } }
Service 接口类,实现类:
package com.demo.service; import com.demo.bean.Books; import java.util.List; //BookService:底下需要去实现,调用dao层 public interface BookService { //增加一个Book int addBook(Books book); //根据id删除一个Book int deleteBookById(int id); //更新Book int updateBook(Books books); //根据id查询,返回一个Book Books queryBookById(int id); //查询全部Book,返回list集合 List<Books> queryAllBook(); }
package com.demo.service.impl; import com.demo.bean.Books; import com.demo.mapper.BookMapper; import com.demo.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookServiceImpl implements BookService { //调用mapper层的操作 @Autowired private BookMapper bookMapper; public int addBook(Books book) { return bookMapper.addBook(book); } public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } public int updateBook(Books books) { return bookMapper.updateBook(books); } public Books queryBookById(int id) { return bookMapper.queryBookById(id); } public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } }
Mapper 接口类:
package com.demo.mapper; import com.demo.bean.Books; import java.util.List; public interface BookMapper { //增加一个Book int addBook(Books book); //根据id删除一个Book int deleteBookById(int id); //更新Book int updateBook(Books books); //根据id查询,返回一个Book Books queryBookById(int id); //查询全部Book,返回list集合 List<Books> queryAllBook(); }
以及Mapper.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.demo.mapper.BookMapper"> <!--增加一个Book--> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books(bookName,bookCounts,detail) values (#{bookName}, #{bookCounts}, #{detail}) </insert> <!--根据id删除一个Book--> <delete id="deleteBookById" parameterType="int"> delete from ssmbuild.books where bookID=#{bookID} </delete> <!--更新Book--> <update id="updateBook" parameterType="Books"> update ssmbuild.books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID} </update> <!--根据id查询,返回一个Book--> <select id="queryBookById" resultType="Books"> select * from ssmbuild.books where bookID = #{bookID} </select> <!--查询全部Book--> <select id="queryAllBook" resultType="Books"> SELECT * from ssmbuild.books </select> </mapper>
6,创建配置文件
jdbc.properties文件:
jdbc_myql.user=root jdbc_myql.password=root jdbc_myql.driverClass=com.mysql.jdbc.Driver jdbc_myql.jdbcUrl=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8 #注意:上面红色标记的是自己的数据库用户名,密码和库名。 #--连接池 #---连接池中保留的最大连接数 默认值: 15---- jdbc.maxPoolSize=30 #---连接池中保留的最小连接数,默认为:3--- jdbc.minPoolSize=4 #!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-- jdbc.initialPoolSize=3 #--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -- jdbc.maxIdleTime=60 #--后等待获取新连接的时间,超时后将抛出SQLException jdbc.checkoutTimeout=5000 #--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 jdbc.acquireIncrement=5
springContext.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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- IOC 容器包结构扫描 --> <context:component-scan base-package="com.demo"/> <!-- 加载perprotites核心数据库配置文件 --> <context:property-placeholder location="classpath*:/config/jdbc.properties"/> <!-- 配置数据库数据源连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user"><value>${jdbc_myql.user}</value></property> <property name="password"><value>${jdbc_myql.password}</value></property> <property name="driverClass"><value>${jdbc_myql.driverClass}</value></property> <property name="jdbcUrl"><value>${jdbc_myql.jdbcUrl}</value></property> <property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property> <property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property> <property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property> <property name="maxIdleTime"><value>${jdbc.maxIdleTime}</value></property> <property name="checkoutTimeout"><value>${jdbc.checkoutTimeout}</value></property> <property name="acquireIncrement"><value>${jdbc.acquireIncrement}</value></property> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 注入数据源、给实体类起别名(默认是类类名称)、加载SQLmapper映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.demo.bean"/> <property name="mapperLocations" value="classpath*:com/demo/mapper/*Mapper.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.demo.mapper" p:sqlSessionFactoryBeanName="sqlSessionFactory"/> </beans>
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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- IOC 容器包结构扫描 --> <context:component-scan base-package="com.demo"/> <!-- 加载perprotites核心数据库配置文件 --> <context:property-placeholder location="classpath*:/config/jdbc.properties"/> <!-- 配置数据库数据源连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user"><value>${jdbc_myql.user}</value></property> <property name="password"><value>${jdbc_myql.password}</value></property> <property name="driverClass"><value>${jdbc_myql.driverClass}</value></property> <property name="jdbcUrl"><value>${jdbc_myql.jdbcUrl}</value></property> <property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property> <property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property> <property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property> <property name="maxIdleTime"><value>${jdbc.maxIdleTime}</value></property> <property name="checkoutTimeout"><value>${jdbc.checkoutTimeout}</value></property> <property name="acquireIncrement"><value>${jdbc.acquireIncrement}</value></property> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 注入数据源、给实体类起别名(默认是类类名称)、加载SQLmapper映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.demo.bean"/> <property name="mapperLocations" value="classpath*:com/demo/mapper/*Mapper.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.demo.mapper" p:sqlSessionFactoryBeanName="sqlSessionFactory"/> </beans>
7,在WEB-INF下添加jsp包:
8,配置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"> <!--DispatcherServlet控制器--> <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*:config/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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/springContext.xml</param-value> </context-param> <!--监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--编码过滤encodingFilter--> <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> <!--Session过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
9.创建数据库ssmbuild
1,创建数据库语法:create database ssmbuild;
注意:修改数据库编码utf-8
2,创建,设计表( 注意:数据库和表的编码格式都是:utf-8)
3,插入数据
10,配置tomcat测试
启动没问题
浏览器访问:
点击进入页面,查询出数据
总结:
1.以上是我总结的ssm框架基础整合
2.如果有其他功能实现,再加配置和依赖
3.希望大家喜欢,有问题可以私信或者评论区问我!!!