Maven+SSM框架项目实例——IDEA
Posted 答辉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven+SSM框架项目实例——IDEA相关的知识,希望对你有一定的参考价值。
一、项目环境
- 开发系统:Window10
- 开发工具:IDEA
- JDK:1.8
- 框架:Maven+Spring+SpringMVC+Mybatis
- 数据库:mysql
二、项目结构
项目文件架构:
三、Maven配置
pom.xml
<properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</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-webmvc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.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.2</version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.2</version> </dependency> </dependencies>
四、SSM的配置文件
mybatis-config.xml
<!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"/> </settings> <!-- 只需配置别名,其他的配置使用其他的方法进行配置 --> <typeAliases> <typeAlias type="com.model.entity.Student" alias="Student"/> <typeAlias type="com.model.entity.Teacher" alias="Teacher"/> <typeAlias type="com.model.entity.Classes" alias="Classes"/> </typeAliases> </configuration>
spring-mybatis.xml:
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 导入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 注解的扫描路径 --> <context:component-scan base-package="com.*"/> <!-- 连接池 --> <!-- 配置1个阿里连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName"> <value>${jdbc_driverClassName}</value> </property> <property name="url"> <value>${jdbc_url}</value> </property> <property name="username"> <value>${jdbc_username}</value> </property> <property name="password"> <value>${jdbc_password}</value> </property> <!-- 连接池最大使用连接数 --> <property name="maxActive"> <value>20</value> </property> <!-- 初始化连接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 获取连接最大等待时间 --> <property name="maxWait"> <value>60000</value> </property> <!-- 连接池最大空闲 --> <property name="maxIdle"> <value>20</value> </property> <!-- 连接池最小空闲 --> <property name="minIdle"> <value>3</value> </property> <!-- 自动清除无用连接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout"> <value>180</value> </property> <!-- 连接属性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> </bean> <!-- mybatis的sessionfatory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:/mapping/*.xml"/> <!-- dao的实现类对象,既mapper对象 。从上面的session工厂中,自动创建出所有mapper文件下的实现类对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.model.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory"/> <!-- 事务管理器,用于注解注入事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> </beans>
springmvc-servlet.xml:
<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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 注解的扫描路径 --> <context:component-scan base-package="com.controller"/> <mvc:annotation-driven/> <!-- springmvc的视图解析器 --> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="prefix" value="/WEB-INF/JSP/"></property> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> </beans>
jdbc.properties:
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/xxxx?characterEncoding=utf-8 jdbc_username=xxxx jdbc_password=xxxx
五、数据库表
student表:
classes表:
teacher表:
六、各层代码
实体类
Classes:
package com.model.entity; public class Classes { private int cid; private String cname; @Override public String toString() { return "Classes{" + "cid=" + cid + ", cname=‘" + cname + ‘‘‘ + ‘}‘; } public Classes() { } public Classes(int cid, String cname) { this.cid = cid; this.cname = cname; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
Student:
package com.model.entity; public class Student { private int sid;//学号 private String sname;//姓名 private Classes classes;//所属班级 private String sex;//性别 private String birthplace;//出生地 @Override public String toString() { return "Student{" + "sid=" + sid + ", sname=‘" + sname + ‘‘‘ + ", classes=" + classes + ", sex=‘" + sex + ‘‘‘ + ", birthplace=‘" + birthplace + ‘‘‘ + ‘}‘; } public Student() { } public Student(int sid, String sname, Classes classes, String sex, String birthplace) { this.sid = sid; this.sname = sname; this.classes = classes; this.sex = sex; this.birthplace = birthplace; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthplace() { return birthplace; } public void setBirthplace(String birthplace) { this.birthplace = birthplace; } }
Teacher:
package com.model.entity; public class Teacher { private int tid;//教师编号 private String tname;//姓名 private Classes classes;//所教班级(student的cid外键) public Teacher() { } @Override public String toString() { return "Teacher{" + "tid=" + tid + ", tname=‘" + tname + ‘‘‘ + ", classes=" + classes + ‘}‘; } public Teacher(int tid, String tname, Classes classes) { this.tid = tid; this.tname = tname; this.classes = classes; } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
持久层(Dao类)
StudentDao:
package com.model.dao; import com.model.entity.Student; import org.apache.ibatis.annotations.Param; import java.util.List; public interface StudentDao { public List<Student> findAll();//查找所有学生 public void add(@Param("sname") String sname, @Param("cid") int cid, @Param("sex") String sex, @Param("birthplace") String birthplace);//添加学生 public Student findBySid(int sid);//根据学号查学生 }
package com.model.dao; import com.model.entity.Student; import org.apache.ibatis.annotations.Param; import java.util.List; public interface StudentDao { public List<Student> findAll();//查找所有学生 public void add(@Param("sname") String sname, @Param("cid") int cid, @Param("sex") String sex, @Param("birthplace") String birthplace);//添加学生 public Student findBySid(int sid);//根据学号查学生 }
业务层(Service类)
StudentService:
import com.model.entity.Student; import java.util.List; public interface StudentService { public List<Student> findAll(); public void add(String sname, int cid, String sex, String birthplace);//添加学生 public Student findBySid(int sid);//根据学号查学生 }
StudentServiceImpl:
package com.service.Impl; import com.model.dao.StudentDao; import com.model.entity.Student; import com.model.service.StudentService; import org.springframework.stereotype.Service; import java.util.List; import javax.annotation.Resource; @Service public class StudentServiceImpl implements StudentService { //这里的自动装配会报错,可以忽视,因为在spring-mybatis.xml中有进行配置,通过mapper映射从Session工厂中获取 @Resource private StudentDao studentDao; public List<Student> findAll() { return studentDao.findAll(); } public void add(String sname, int cid, String sex, String birthplace) { } public Student findBySid(int sid) { return studentDao.findBySid(sid); } }
控制器(Controller)
StudentAction:
package com.controller; import com.model.entity.Student; import com.model.service.StudentService; 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 org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class StudentAction { @Autowired private StudentService service;//持有一个业务层对象 @RequestMapping("/findall") public String findAll(Model model) { List<Student> list = service.findAll(); model.addAttribute("studentlist", list); return "show"; } //ajax查询数据 @ResponseBody @RequestMapping("/findbysid") public Student findBySid(@RequestParam("sid") int sid) { return service.findBySid(sid); } }
package com.controller; import com.model.entity.Student; import com.model.service.StudentService; 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 org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class StudentAction { @Autowired private StudentService service;//持有一个业务层对象 @RequestMapping("/findall") public String findAll(Model model) { List<Student> list = service.findAll(); model.addAttribute("studentlist", list); return "show"; } //ajax查询数据 @ResponseBody @RequestMapping("/findbysid") public Student findBySid(@RequestParam("sid") int sid) { return service.findBySid(sid); } }
七、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_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml; classpath:springmvc-servlet.xml </param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <display-name>ssm_student</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
八、JSP页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script> <script language="JavaScript"> $(function () { $("#search").click(function () { $.ajax({ type: "post", url: "findbysid.action", data: $("#sid"), dataType: "json", success: function (data) { var html = ""; html += "<td>" + data.sid + "</td><td>" + data.classes.cname + "</td><td>" + data.sname + "</td><td>" + data.sex + "</td><td>" + data.birthplace + "</td>"; $("#student").html(html); }, //null值不会被success回调函数捕获,因此可以通过error来实现该功能 error: function () { alert("请输入正确的学号!"); } }) }) }) </script> <body> <center> <div style="margin-top: 25px"> 请输入所要查询的学号:<input type="text" id="sid" name="sid"/><input id="search" style="margin-left: 10px" type="button" value="搜索"><br/> </div> <div style="margin-top: 50px"> <table border="1"> <tr> <td>学号</td> <td>班级</td> <td>姓名</td> <td>性别</td> <td>出生地</td> </tr> <tr id="student"> </tr> </table> </div> </center> </body> </html>
九、Tomcat测试
测试结果(成功):
输入1,没有对应学号,则弹出:
以上是关于Maven+SSM框架项目实例——IDEA的主要内容,如果未能解决你的问题,请参考以下文章
使用idea搭建一个简单的SSM框架:使用idea创建maven项目
01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层
02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层
IDEA+SSM+Maven实现商品管理系统(超详细SSM整合项目)
项目笔记: 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架