一、概述:
Spring是一个轻量级开发框架,好比一个大工厂(容器),可以将所有对象的创建和依赖关系交给spring管理。
SpringMVC是一个基于mvc的web框架。spring mvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。
MyBatis是一个持久层的框架,在使用上相比Hibernate更加灵活,可以控制sql的编写,使用 XML或注解进行相关的配置.
二、搭建好项目的运行环境(java环境,Maven,Mysql,Tomcat),所使用工具Eclipse。具体的环境搭建请自行搜一下资料。jdk1.8+tomcat8.5
三、开始搭建
首先看一下项目结构。controller,mapper,pojo,service
1.创建数据库表user,添加一条测试数据
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(40) NOT NULL, `password` varchar(255) NOT NULL, `age` int(4) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; insert into `user_t`(`id`,`user_name`,`password`,`age`) values (1,‘admin‘,‘123456‘,21)
2. pom.xml文件中的包依赖
<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> <groupId>com.iris</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.0.5.RELEASE</spring.version> <mybatis.version>3.2.1</mybatis.version> <log4j.version>1.2.12</log4j.version> <mysql.version>5.1.35</mysql.version> <shiro.version>1.2.4</shiro.version> </properties> <dependencies> <!-- 添加Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</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>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--spring单元测试依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- spring webmvc相关jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- alibaba data source 相关jar包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.23</version> </dependency> <!-- alibaba fastjson 格式化对 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- logback start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!--mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- 添加servlet3.0核心包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.2-b01</version> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--单元测试依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ssm</finalName> </build> </project>
3.配置文件包括
jdbc,properties
jdbc.driver=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123456
log4j.properties
#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B log4j.rootLogger=INFO,Console,File #\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=system.out #\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F %d [%t]%-5p [%c] - %m%n log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n #\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6 log4j.appender.File = org.apache.log4j.RollingFileAppender #\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55 log4j.appender.File.File = f:/logs/ssms.log #\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F log4j.appender.File.MaxFileSize = 10MB # \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 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-4.0.xsd"> <!-- 扫描所有的 controller --> <context:component-scan base-package="com.iris.controller" /> <!-- 启动注解驱动 SpringMVC 功能 --> <mvc:annotation-driven /> <!-- 定义跳转的文件的前后缀,视图模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 静态资源访问(不拦截此目录下的东西的访问) --> <mvc:resources location="/js/" mapping="/js/**" /> </beans>
spring-mybatis.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-4.0.xsd"> <!-- 自动扫描 将标注Spring注解的类自动转化Bean --> <context:component-scan base-package="com.iris" /> <!-- 导入数据库配置文件 --> <context:property-placeholder location="classpath:config/jdbc.properties"/> <!-- druid 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 扫描mybatis 配置文件--> <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/iris/mapper/*.xml" /> </bean> <!-- 扫描Mapper 配置文件--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.iris.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 读取Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/spring-*.xml </param-value> </context-param> <!-- Spring MVC配置 --> <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:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post乱码处理 --> <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> </web-app>
4.pojo实体
package com.iris.pojo; public class User { private Integer id; private String user_name; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
5.dao层mapper接口
package com.iris.mapper; import java.util.List; import com.iris.pojo.User; public interface UserMapper { Boolean addUser(User user); Boolean delUser(Integer id); Boolean modUser(User user); List<User> getUserList(); User getUserById(Integer id); }
<?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.iris.mapper.UserMapper"> <select id="getUserList" resultType="com.iris.pojo.User"> select * from user </select> <select id="getUserById" resultType="com.iris.pojo.User" parameterType="int"> select * from user t where t.id=#{id} </select> </mapper>
6.controller控制层,这里将返回数据类型统一封装到ResultBean
package com.iris.controller; import java.io.Serializable; import org.springframework.web.bind.annotation.RestController; @RestController public class ResultBean<T> implements Serializable{ private static final long serialVersionUID=1L; private static final int NO_LOGIN=-1; private static final int SUCCESS=0; private static final int FAIL=1; private static final int NO_PERMISSION=2; private String msg="success"; private int code=SUCCESS; private T data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public ResultBean() { super(); } public ResultBean(T data) { super(); this.data=data; } public ResultBean(Throwable e) { super(); this.msg=e.toString(); this.code=FAIL; } }
package com.iris.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.iris.pojo.User; import com.iris.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/add") public ResultBean<Boolean> addUser(User user){ return new ResultBean<Boolean>(userService.addUser(user)); } @RequestMapping("/del") public ResultBean<Boolean> delUser(Integer id){ return new ResultBean<Boolean>(userService.delUser(id)); } @RequestMapping("/mod") public ResultBean<Boolean> modUser(User user){ return new ResultBean<Boolean>(userService.modUser(user)); } @RequestMapping("/getAll") public ResultBean<List<User>> getUserList(){ return new ResultBean<List<User>>(userService.getUserList()); } @RequestMapping("/getUser") public ResultBean<User> getUserById(Integer id){ return new ResultBean<User>(userService.getUserById(id)); } }
7.service服务接口以及实现类
package com.iris.service; import java.util.List; import com.iris.pojo.User; public interface UserService { Boolean addUser(User user); Boolean delUser(Integer id); Boolean modUser(User user); List<User> getUserList(); User getUserById(Integer id); }
package com.iris.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.iris.mapper.UserMapper; import com.iris.pojo.User; import com.iris.service.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public Boolean addUser(User user) { return userMapper.addUser(user); } public Boolean delUser(Integer id) { return userMapper.delUser(id); } public Boolean modUser(User user) { return userMapper.modUser(user); } public User getUserById(Integer id) { return userMapper.getUserById(id); } public List<User> getUserList() { return userMapper.getUserList(); } }
以上只是一个小白查阅了相关资料做的一个总结,如有问题敬请指正。
原文地址:https://www.cnblogs.com/JonlyZhang/p/9866608.html
时间: 2024-11-05 12:23:03