最近有空所以把以前做项目的框架做个简单的整合
1.创建Maven项目,导入依赖
<dependencies>
<!--单元测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!--jstl依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mysql数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--c3p0依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mybatis-spring依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- jedis (一个redis client端的jar)-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>
<build>
<finalName>ssm_redis</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.根据个人习惯创建包
3.在resources中创建db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/Bloggingplatform?characterEncoding=utf-8&allowMultiQueries=true
db.user=root
db.password=@838061522@ly
#定义初始连接数
initPoolSize=3
#定义最大连接数
maxPoolSizes=30
4.在resources中创建spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描:自动扫描com.heyang.ssm包下所有类的注解-->
<context:component-scan base-package="work.javaj"/>
<!--加载db.properties-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置数据源 从c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
<property name="user" value="${db.user}"></property>
<property name="password" value="${db.password}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSizes}"></property>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载MyBatis配置文件-->
<!--<property name="configLocation" value="classpath:mybatis-Config.xml"></property>-->
<!--加载所有映射文件-->
<property name="mapperLocations" value="classpath:work/javaj/mappers/*.xml"></property>
<!--typeAliasesPackage对应实体类所在的包,这个时候会自动取包名作为别名-->
<property name="typeAliasesPackage" value="work.javaj.pojo"></property>
</bean>
<!--配置Dao层:spring注入这个bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--将sqlSessionFactory注入com.zking.dao包的类中,dao包中的类直接使用sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="work.javaj.dao"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="expand*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
</beans>
5.在pojo中创建实体类
public class Article implements Serializable{
private static final long serialVersionUID = 1L;
private String aId; //文章id
private String aTitle; //标题
private String aContent; //内容
private String aTitleImg; //标题图片
private String aTime; //时间
private Integer aReadingQuantity; //阅读量
private Integer aCommentnum; //评论数
private Programa programa;//栏目id 外键
private String label;//标签
private Administrator administrator;//管理员id 外键
private Integer aStatusBar; //状态 0不公开 1公开
private String aAbstract;//摘要
//此处省略setter和getter
}
6.创建dao接口 ArticleDao
public interface ArticleDao {
/*
* @Author Uncle Liu
* @Description //TODO 根据id查询Article
* @Date 18:36 2018/10/30
**/
Article getArticleById(String aId);
}
7.创建service接口及实现类
7.1 创建ArticleService
public interface ArticleService {
/*
* @Author Uncle Liu
* @Description //TODO 根据id查询Article
* @Date 18:36 2018/10/30
**/
Article getArticleById(String aId);
}
7.2 创建ArticleImpl
@Service("articleImpl")
public class ArticleImpl implements ArticleService {
@Resource
private ArticleDao articleDao;
@Override
public Article getArticleById(String aId) {
return articleDao.getArticleById(aId);
}
}
8.在mappers中创建ArticleMapper.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="work.javaj.dao.ArticleDao">
<!--映射Article对象的resultMap(结果映射集)-->
<resultMap id="articleMapper" type="work.javaj.pojo.Article">
<id property="aId" column="a_id" />
<result property="aTitle" column="a_title" />
<result property="aContent" column="a_content" />
<result property="aTitleImg" column="a_title_img" />
<result property="aTime" column="a_time" />
<result property="aReadingQuantity" column="a_reading_quantity" />
<result property="aCommentnum" column="a_commentnum" />
<result property="aStatusBar" column="a_status_bar" />
<result property="label" column="label" />
<result property="aAbstract" column="a_abstract" />
<!--<association property="administrator" column="u_id" select="work.javaj.dao.AdministratorDao.getAdministratorById"/>-->
<!--<association property="programa" column="p_id" select="work.javaj.dao.ProgramaDao.getProgramaById"/>-->
</resultMap>
<!--根据id查询-->
<select id="getArticleById" parameterType="String" resultMap="articleMapper">
select a_abstract,a_id,a_title,a_content,a_title_img,a_time,a_reading_quantity,a_commentnum,p_id,label,a_status_bar,u_id from article where a_id=#{aId}
</select>
</mapper>
9.配置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"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring整合mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--配置编码过滤器 防止乱码-->
<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>
<!--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:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
10.创建Controller
@Controller
@RequestMapping("/index")
public class indexController {
@Resource
private ArticleService articleService;
@RequestMapping("/a")
@ResponseBody
public String index(){
//根据id查询
Article article = articleService.getArticleById("91cf33d3-2b96-45e8-a66d-ffd7fabb8de8");
System.out.println(article);
return "Hello";
}
}
11.创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/index/a.do">你好</a>
</body>
</html>
12.测试
12.1 运行结果:
12.2 控制台输出结果:
12.3 页面显示
评论 (0)