選擇maven(下圖是用idea自帶的maven,你也能夠用你本身下載的maven,directory地址指向你本身的maven就行,setting和repository同理) html
默認就能夠了剛開始時間比較長,能夠看到,用了有7分多鐘, 仍是很快的 java
爲了以後可以快速的下載依賴包,咱們要加一個官方庫的鏡像,由於maven的官方倉庫在國外,太慢了。 咱們在你的.m2文件夾下的setting.xml中添加以下代碼:mysql
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
複製代碼
具體位置以下圖所示 git
maven包下載好後,項目結構以下 github
能夠看到, 缺乏java包,在main上右鍵新建包 web
建完java後的目錄到這裏先不要修改java包的狀態, 接着建立其餘目錄結構,這麼作的主要目的是方便建立分級結構。spring
當改變java包的狀態後,service.impl會變成一個包的名字,這時候須要Rename如下這個包,先變成service,而後建立PersonService.java, 在建立impl包, 就行了sql
若是以爲麻煩能夠在建立目錄的時候先不建立impl包 數據庫
而後修改java包的狀態,右鍵java包接下來建立一系列的文件, 而後在進行配置,先把骨架搭起來 spring-mvc
以上就是全部用到的文件,如今開始一個一個文件填充, 如今的目的是搭建起來能運次的項目,若是想知道配置原理請自行查閱資料被標稱黃色的部分是須要注意的, 若是更改了須要作相應的改動, 若是項目目錄跟這個同樣,就不須要改動
PersonController.java
package com.ssm.demo.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssm.demo.entity.Person;
import com.ssm.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/selectPerson")
public void selectPerson(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
long personId = Long.parseLong(request.getParameter("id"));
Person person =personService.findPersonById(personId);
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(person));
response.getWriter().close();
}
}
複製代碼
PersonMapperDao.java
package com.ssm.demo.dao;
import com.ssm.demo.entity.Person;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonMapperDao {
Person findPersonById(long id);
}
複製代碼
Person.java
package com.ssm.demo.entity;
public class Person {
private long id;
private String name;
private String email;
private int status;
//getter setter
@Override
public String toString(){
return this.getId()+"---"+this.getName()+"---"+
this.getEmail()+"---"+this.getStatus();
}
}
複製代碼
PersonService.java
package com.ssm.demo.service;
import com.ssm.demo.entity.Person;
public interface PersonService {
Person findPersonById(long id);
}
複製代碼
PersonServiceImpl.java
package com.ssm.demo.service.impl;
import com.ssm.demo.dao.PersonMapperDao;
import com.ssm.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapperDao personMapperDao;
public Person findPersonById(long id) {
return personMapperDao.findPersonById(id);
}
}
複製代碼
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.ssm.demo"/>
<!-- 開啓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=".html"/>
</bean>
</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"
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.ssm.demo"/>
<!-- 配置數據庫相關參數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.ssm.demo.entity"/>
<!-- 掃描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.ssm.demo.dao"/>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入數據庫鏈接池 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置基於註解的聲明式事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
複製代碼
PersonMapper.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.ssm.demo.dao.PersonMapperDao">
<select id="findPersonById" resultType="Person" parameterType="long">
SELECT * FROM person WHERE id = #{id}
</select>
</mapper>
複製代碼
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
#數據庫地址
jdbc.url=jdbc:mysql://localhost:3306/數據庫名?useUnicode=true&characterEncoding=utf8
#用戶名
jdbc.username=數據庫帳號
#密碼
jdbc.password=數據庫密碼
#最大鏈接數
c3p0.maxPoolSize=30
#最小鏈接數
c3p0.minPoolSize=10
#關閉鏈接後不自動commit
c3p0.autoCommitOnClose=false
#獲取鏈接超時時間
c3p0.checkoutTimeout=10000
#當獲取鏈接失敗重試次數
c3p0.acquireRetryAttempts=2
複製代碼
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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>Archetype Created Web Application</display-name>
<!-- 編碼過濾器 -->
<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:/config/spring/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>
複製代碼
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", "person/selectPerson", 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="selectUser()">onclick test</button>
</body>
</html>
複製代碼
先clean一下, 而後package一下
打包成功後,控制檯顯示 會多一個包,和一個war文件 配置tomcat 指定tomcat名稱,地址,和訪問路徑 選擇deployment, 點擊下面的加號 點擊Artifact,彈窗後點擊第二個分解的exploded輸入請求的跟地址, 項目名
navicat 圖形化界面建立數據庫,而後修改jdbc.properties文件的數據庫名稱爲本身建立的數據庫,而後建立表
CREATE TABLE person (
id int(11) NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
name varchar(255) NOT NULL,
status int(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO person VALUES ('1', '123456qq.com', 'zzz', '0');
SET FOREIGN_KEY_CHECKS=1;
複製代碼