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的更多相关文章
- [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World
来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...
- 01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层
作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 这是一个整合IDEA+Maven+SSM框架的高并发的商品秒杀项目.我们将分为以下几篇文章来进行详 ...
- Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)
[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...
- Maven+SSM框架实现简单的增删改查
Spring介绍: spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情.然而, Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Ja ...
- Maven+SSM框架(Spring+SpringMVC+MyBatis)(二)
1.基本概念 2.开发环境搭建 3.Maven Web项目创建 4.SSM整合 此次整合我分两个配置文件: 1)分别是spring-mybatis.xml,包含spring和mybatis的配置文件, ...
- 使用Maven+ssm框架搭建一个web项目
1,前期准备:Eclipse(Mars.2 Release (4.5.2)).jdk1.7.tomcat7.maven3.2.1 2.使用eclipse中的maven新建一个web项目 点击next: ...
- 史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)
目录 前言: 1. 搭建整合环境 2.Spring框架代码的编写 3.SpringMVC框架代码的编写 4. Spring整合SpringMVC的框架 5.MyBatis框架代码的编写 6. Spri ...
- Maven+SSM框架搭建【spring+springmvc+mybatis】
本案例用到:ssm[spring+springmvc+mybatis]框架 数据库:mysql (推荐使用mysql 或者 sqlserver .oracle太大,一般大型项目才会用到) 开发工具: ...
- IDEA + Maven + SSM 框架整合步骤
因为前段时间自己想写个SSM的demo,然而不知怎么回事,配置完之后出现错误,怎么都调不好.最后从朋友那里拷了一个SSM的demo过来搭建成功,写这篇东西也是为了以后如果还有需要可以方便的查阅,并且也 ...
随机推荐
- 多线程的使用:只能用cmd来玩不能用idle
from multiprocessing import Process import os def pro_do(name, func): print () if __name__ == " ...
- php-生成数据库设计文档
在线以及提供下载数据库设计文档 $dbserver = "192.168.128.190:42578"; $dbusername = "root"; $dbpa ...
- 开始Java之旅
从今天起,cgg将给大家讲讲Java这种神奇的东西. 至于配置环境变量,大家可以看看我的博客:环境变量上面有详细解释. 下面先给大家一个公式: public class [文件名]{ ...
- k8s的port、targetport、nodeport之间的区别
先看举例: k8s集群中跑着一个tomcat服务,tomcat容器expose的端口为8080 apiVersion: v1 kind: Service metadata: name: tomcat- ...
- Linux服务器部署系列之八—Sendmail篇
Sendmail是目前Linux系统下面用得最广的邮件系统之一,虽然它存在一些不足,不过,目前还是有不少公司在使用它.对它的学习,也能让我们更深的了解邮件系统的运作.下面我们就来看看sendmail邮 ...
- 使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
背景: yum包的管理是使用python写的,有对应的python版本 遇到的问题报错如下: File "/usr/bin/yum", line 30 except K ...
- PHP数据库抽象层--PDO(PHP Data Object) [一]
1.简介:(PDO数据库访问抽象层,统一各种 数据库的访问接口 ) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库 ...
- SpringMvc中controller之间的方法调用
方法一, return new ModelAndView("redirect:"+新地址); 方法二, response.sendRedirect(新地址); return nul ...
- IDEA14添加SVN
今天升级IDEA14,发现SVN变了,折腾了好一会才搞好.记录一下,希望其他人能少走弯路.主要是IDEA14的svn是用命令行工具进行操作,这样对以后SVN的兼容性也会好很多,这个在他们官方的博客上也 ...
- spring MVC controller中的方法跳转到另外controller中的某个method的方法
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...