使用idea2017搭建SSM框架(图文步骤)
这篇文章主要介绍了使用idea2017搭建SSM框架(图文步骤),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧搭建个ssm框架居然花费了我好长时间!特此记录!
需要准备的环境:
[*]idea 2017.1
[*]jdk1.8
[*]maven 3.3.9
请提前将idea与maven、jdk配置好,本次项目用的都是比较新的
注:配置完ide红线报错没关系!可以run!
步骤:
一、首先使用idea新建一个maven webapp项目
点击finish,第一次搭建可能会很慢,甚至可能需要vpn才能搭建成功
二、搭建目录结构
我这里列出的是搭建完了之后所有的目录和文件,诸位先把目录文件建起来,然后我在给出文件内容
这里的目录建好之后还需要设置一下,让idea识别目录作用,选择file-project structure
设置完成后ok
三、配置文件内容
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.chatrobot</groupid>
<artifactid>chatrobot</artifactid>
<packaging>war</packaging>
<version>1.0-snapshot</version>
<name>chatrobot maven webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 设置项目编码编码 -->
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
<!-- spring版本号 -->
<spring.version>4.3.5.release</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.1</mybatis.version>
</properties>
<dependencies>
<!-- java ee -->
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.12</version>
</dependency>
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupid>ch.qos.logback</groupid>
<artifactid>logback-classic</artifactid>
<version>1.2.2</version>
</dependency>
<!-- json -->
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>2.8.7</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupid>com.mchange</groupid>
<artifactid>c3p0</artifactid>
<version>0.9.5.2</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.3.1</version>
</dependency>
<!-- 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-jdbc</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-web</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-test</artifactid>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalname>chatrobot</finalname>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<!-- 设置jdk版本 -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意右下角更新pom
logback.xml
<?xml version="1.0" encoding="utf-8"?>
<configuration debug="true">
<appender name="stdout" class="ch.qos.logback.core.consoleappender">
<encoder>
<pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="stdout"/>
</root>
</configuration>
这里可以控制输出格式和内容,有兴趣的可以自己设置
jdbc.properties
jdbc.driver=com.mysql.jdbc.driver
#数据库地址
jdbc.url=jdbc:mysql://xxxxxxxxx:3306/chatrobot?useunicode=true&characterencoding=utf8
#用户名
jdbc.username=xxxx
#密码
jdbc.password=xxxxx
#最大连接数
c3p0.maxpoolsize=30
#最小连接数
c3p0.minpoolsize=10
#关闭连接后不自动commit
c3p0.autocommitonclose=false
#获取连接超时时间
c3p0.checkouttimeout=10000
#当获取连接失败重试次数
c3p0.acquireretryattempts=2
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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.chatrobot.service"/>
<!-- 配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据库连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource">
<property name="driverclass" value="${jdbc.driver}"/>
<property name="jdbcurl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxpoolsize" value="${c3p0.maxpoolsize}"/>
<property name="minpoolsize" value="${c3p0.minpoolsize}"/>
<property name="autocommitonclose" value="${c3p0.autocommitonclose}"/>
<property name="checkouttimeout" value="${c3p0.checkouttimeout}"/>
<property name="acquireretryattempts" value="${c3p0.acquireretryattempts}"/>
</bean>
<!-- 配置sqlsessionfactory对象 -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<!-- 注入数据库连接池 -->
<property name="datasource" ref="datasource"/>
<!-- 扫描model包 使用别名 -->
<property name="typealiasespackage" value="com.chatrobot.model"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperlocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置扫描dao接口包,动态实现dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
<!-- 注入sqlsessionfactory -->
<property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/>
<!-- 给出需要扫描dao接口包 -->
<property name="basepackage" value="com.chatrobot.dao"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
<!-- 注入数据库连接池 -->
<property name="datasource" ref="datasource"/>
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionmanager"/>
</beans>
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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 扫描web相关的bean -->
<context:component-scan base-package="com.chatrobot.controller"/>
<!-- 开启springmvc注解模式 -->
<mvc:annotation-driven/>
<!-- 静态资源默认servlet配置 -->
<mvc:default-servlet-handler/>
<!-- 配置jsp 显示viewresolver -->
<bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
<property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/>
<property name="prefix" value="/web-inf/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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_3_1.xsd"
version="3.1">
<display-name>chatrobot</display-name>
<description>chatrobot_alpha_0.0.1</description>
<!-- 编码过滤器 -->
<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>
<!-- 配置dispatcherservlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
<!-- 配置springmvc需要加载的配置文件-->
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
到这里基本上环境就搭建完成了,下面开始测试
四、测试
先导入一份数据库测试文件
create table `user` (
`id` int(11) not null auto_increment comment '用户id',
`email` varchar(255) not null comment '用户邮箱',
`password` varchar(255) not null comment '用户密码',
`username` varchar(255) not null comment '用户昵称',
`role` varchar(255) not null comment '用户身份',
`status` int(1) not null comment '用户状态',
`regtime` datetime not null comment '注册时间',
`regip` varchar(255) not null comment '注册ip',
primary key (`id`),
unique key `email` (`email`) using btree
) engine=innodb auto_increment=2 default charset=utf8;
insert into `user` values ('1', 'xxx', 'xxxxx', 'xxxxx', 'root', '0', '2017-03-28 09:40:31', '127.0.0.1');
set foreign_key_checks=1;
接下来配置类
usercontroller
package com.chatrobot.controller;
import javax.servlet.http.httpservletrequest;
import com.chatrobot.model.user;
import com.chatrobot.service.iuserservice;
import com.fasterxml.jackson.databind.objectmapper;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import javax.annotation.resource;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
@controller
@requestmapping("/user")
public class usercontroller {
@resource
private iuserservice userservice;
@requestmapping("/showuser.do")
public void selectuser(httpservletrequest request, httpservletresponse response) throws ioexception {
request.setcharacterencoding("utf-8");
response.setcharacterencoding("utf-8");
long userid = long.parselong(request.getparameter("id"));
user user = this.userservice.selectuser(userid);
objectmapper mapper = new objectmapper();
response.getwriter().write(mapper.writevalueasstring(user));
response.getwriter().close();
}
}
iuserdao
package com.chatrobot.dao;
import com.chatrobot.model.user;
public interface iuserdao {
user selectuser(long id);
}
user
package com.chatrobot.model;
import java.util.date;
public class user {
private long id;
private string email;
private string password;
private string username;
private string role;
private int status;
private date regtime;
private string regip;
public long getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getemail() {
return email;
}
public void setemail(string email) {
this.email = email;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getrole() {
return role;
}
public void setrole(string role) {
this.role = role;
}
public int getstatus() {
return status;
}
public void setstatus(int status) {
this.status = status;
}
public date getregtime() {
return regtime;
}
public void setregtime(date regtime) {
this.regtime = regtime;
}
public string getregip() {
return regip;
}
public void setregip(string regip) {
this.regip = regip;
}
}
iuserservice
package com.chatrobot.service;
import com.chatrobot.model.user;
public interface iuserservice {
public user selectuser(long userid);
}
userserviceimpl
package com.chatrobot.service.impl;
import com.chatrobot.dao.iuserdao;
import com.chatrobot.model.user;
import com.chatrobot.service.iuserservice;
import org.springframework.stereotype.service;
import javax.annotation.resource;
@service("userservice")
public class userserviceimpl implements iuserservice {
@resource
private iuserdao userdao;
public user selectuser(long userid) {
return this.userdao.selectuser(userid);
}
}
userdao.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">
<!-- 设置为iuserdao接口方法提供sql语句配置 -->
<mapper namespace="com.chatrobot.dao.iuserdao">
<select id="selectuser" resulttype="user" parametertype="long">
select * from user where id = #{id}
</select>
</mapper>
然后新建个测试类,来测试mybatis
iuserdaotest
package com.chatrobot.dao;
import com.chatrobot.model.user;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;
// 加载spring配置文件
@runwith(springjunit4classrunner.class)
@contextconfiguration({"classpath:spring-mybatis.xml"})
public class iuserdaotest {
@autowired
private iuserdao dao;
@test
public void testselectuser() throws exception {
long id = 1;
user user = dao.selectuser(id);
system.out.println(user.getusername());
}
}
运行后结果应该是会在控制台输出id为1的用户名,没成功的话就找bug去吧...
继续新建个页面来测试springmvc和mybatis
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<script>
function selectuser() {
var xmlhttp = new xmlhttprequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
document.getelementbyid("test").innerhtml = xmlhttp.responsetext;
}
}
xmlhttp.open("post", "user/showuser.do", true);
xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");
xmlhttp.send("id=1");
}
</script>
<body>
<p id="test">hello world!</p>
<button type="button" >onclick test</button>
</body>
</html>
新建完成后配置项目运行环境,点击run-edit configurations...
点击加号新建运行环境,选择tomcat server-local
选中新建好的服务器,右边选择deployment,点击加号-atifact...
选择第二项
然后在右边application context配置你的项目名
最后运行项目,在打开的页面中点击按钮测试,成功的话会在页面上显示id为1的用户信息
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
原文链接:https://www.cnblogs.com/hackyo/p/6646051.html
http://www.zzvips.com/article/173093.html
页:
[1]