输入项目名和工程id(这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样然后next)
选择maven(下图是用idea自带的maven,你也可以用你自己下载的maven,directory地址指向你自己的maven就行,setting和repository同理)
默认就可以了
刚开始时间比较长,可以看到,用了有7分多钟, 还是很快的
为了之后能够快速的下载依赖包,我们要加一个官方库的镜像,因为maven的官方仓库在国外,太慢了。 我们在你的.m2文件夹下的setting.xml中添加如下代码:
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> 复制代码
具体位置如下图所示
maven包下载好后,项目结构如下
可以看到, 缺少java包,在main上右键新建包
建完java后的目录
到这里先不要修改java包的状态, 接着创建其他目录结构,这么做的主要目的是方便创建分级结构。
当改变java包的状态后,service.impl会变成一个包的名字,这时候需要Rename以下这个包,先变成service,然后创建PersonService.java, 在创建impl包, 就好了
如果觉得麻烦可以在创建目录的时候先不创建impl包
然后修改java包的状态,右键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(); } } 复制代码
package com.ssm.demo.dao; import com.ssm.demo.entity.Person; import org.springframework.stereotype.Repository; @Repository public interface PersonMapperDao { Person findPersonById(long id); } 复制代码
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(); } } 复制代码
package com.ssm.demo.service; import com.ssm.demo.entity.Person; public interface PersonService { Person findPersonById(long id); } 复制代码
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); } } 复制代码
<?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> 复制代码
<?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> 复制代码
<?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.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 复制代码
<!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> 复制代码
<!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; 复制代码
大功告成。