一直想自己搭个后台框架来玩玩,只是惰性这个东西往往占据了上风,值此新春佳节,风和日丽,艳阳高照之际,就开始自己的博客之旅吧。
首先先来了解一下SSM这三个东西!
(1)spring:是一个轻量级的java开发框架,简单来说,就是一个轻量级的IOC和AOP框架。轻量级指的是Spring框架在系统初始化时,并没有调用所有的服务,占用资源少。
(2)springMVC:一个轻量级的,基于响应请求的mvc框架。
(3)mybatis: 一个基于Java的持久层框架。它是一个不完全的ORM(对象关系映射)框架,需要程序员自己写sql,带来的好处就是sql的修改和优化比较方便。
废话不多说(主要是本人对这3块的理解不够深,怕误人子弟,等修炼完再来开题_),下面就直接上步骤了。
需要的开发环境
(1)JDK你得有吧,java程序员必备,就不说了
(2)IDEA也得有吧,现在的主流编辑器了
(3)maven,这个神器管理jar包实在是方便,现在越来越能体会到了
(4)git,这个看自己了,需要就上传到github上,方便留存
很崩溃的告诉大家,csdn写博客上传图片实在是太麻烦了,要先截图,然后保存到本地,然后在上传到csdn,真是无力吐槽,我印象中csdn应该是一个大型的玩意,怎么用户体验如此之差,有没有好的写技术博客的可以推荐的?
(1)打开idea,新建一个project,左侧选择maven项目,右侧勾上create from archetype复选框,这个是用来生成固定的项目骨架的,下面列表中有很多的项目模版,我们选择maven-archetype-webapp
(2)点击下一步,输入GroupID和ArtifactId,用过maven创建项目的应该都很清楚这两个的意思了,这里就不赘述了。
这里有一点需要注意的是需要添加一个参数
(3)点击下一步,输入项目名称和项目保存目录,然后finished就创建好了。
这里需要注意的是要多配置一个参数,不然创建项目骨架的时候会很慢,
这只是其中一种解决方法,还有其他方式可以自行百度archetypeCatalog
(4)创建好的项目的src包下面是没有java目录的,我们手动建一个,我们还需要创建一些package出来,最终的目录结构大致如下,当然,大家也可以按自己熟悉的方式来建立项目目录结构
(5)之后我们需要让idea能识别到这些目录,因此需要设置一下source,resource,tests,test resource
maven管理jar包的方式就是在pom文件中引入需要的jar包,完美解决jar依赖问题。需要用到的jar包如下:
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.0.2.RELEASEspring.version>
<mybatis.version>3.2.6mybatis.version>
<slf4j.version>1.7.7slf4j.version>
<log4j.version>1.2.17log4j.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-oxmartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-apiartifactId>
<version>7.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.30version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.1.41version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-mapper-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.9version>
dependency>
dependencies>
我们需要连接数据库进行持久化操作以及日志记录
jdbc配置文件如下:
jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=root
#密码
jdbc.password=
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2
日志配置文件如下:
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
下面我们编写spring-mybatis.xml配置文件来整合spring和mybatis
<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">
<context:component-scan base-package="com.virgil.service"/>
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.virgil.model"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.virgil.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文件来整合spring和springMVC
<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">
<context:component-scan base-package="com.virgil.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<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-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>myssmdisplay-name>
<description>myssm_Alpha_0.0.1description>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-*.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
<async-supported>trueasync-supported>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
(1)我们需要测试一下spring和mybatis的整合是否成功
首先我们需要在数据库中建立一张测试用表
DROP TABLE IF EXISTS `user_t`;
CREATE TABLE `user_t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(40) NOT NULL,
`password` varchar(255) NOT NULL,
`age` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `user_t` */
insert into `user_t`(`id`,`user_name`,`password`,`age`) values (1,'测试','sfasgfaf',24);
然后利用MyBatis Generator自动创建代码(包括实体类,dao层查询接口以及映射xml文件)
下载mybatis-generator-core-1.3.2的jar包,注意网上下载的好多jar包都是里面打开都是class文件,这种不是我们要的,下面这样的才是
打开lib文件夹是这样子的
其实src文件夹是自己新建的,用来接受生成的文件
generatorConfig.xml是自带的一个配置文件,我们需要去修改它,修改内容如下:
<generatorConfiguration>
<classPathEntry location="mysql-connector-java-5.1.25-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/demo" userId="root" password="">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="domain" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="IDao" targetProject="src">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
,start.bat是自己写的处理脚本,执行生成文件的命令,内容如下:
执行完脚本之后就会在src目录下生成3个文件夹
里面分别是实体类,dao接口类,以及接口类的实现(xml文件)
将这三个文件分别拷贝到项目的如下目录下:
其中各个文件的内容如下:
User类:
package com.virgil.model;
/**
* Created by Administrator on 2017/10/15 0015.
*/
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserDao类
package com.virgil.dao;
import com.virgil.model.User;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2017/10/15 0015.
*/
public interface UserDao {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
UserDao.xml
其实当初生成的文件名是UserMapper.xml,为了和UserDao的接口保持一致,所有就手动修改了名字。
注意,这个文件里有个坑需要注意:
namespace的值需要和对应的接口类保持一致
resultMap的type类型需要和对应的实体类保持一致
version="1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"com.virgil.dao.UserDao" >
id="BaseResultMap" type="User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
id="Base_Column_List" >
id, user_name, password, age
id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user_t
where id = #{id,jdbcType=INTEGER}
id="insert" parameterType="User" >
insert into user_t (id, user_name, password,
age)
values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
id="insertSelective" parameterType="User" >
insert into user_t
"(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
if>
<if test="userName != null" >
user_name,
if>
<if test="password != null" >
password,
if>
<if test="age != null" >
age,
if>
"values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
if>
id="updateByPrimaryKeySelective" parameterType="User" >
update user_t
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
if>
set>
where id = #{id,jdbcType=INTEGER}
id="updateByPrimaryKey" parameterType="User" >
update user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
这里我补一个看起来更高大上一点的方法(上次写完之后一直感觉前面那个方法和项目有点脱离,所以后来用搜寻到这个方法):
(1)在pom.xml文件中配置如下:
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.2version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xmlconfigurationFile>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
<executions>
<execution>
<id>Generate MyBatis Artifactsid>
<goals>
<goal>generategoal>
goals>
execution>
executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.30version>
<scope>runtimescope>
dependency>
dependencies>
plugin>
(2)在copy一个generatorConfig.xml文件放到src/main/resources/mybatis-generator下面
(3)generatorConfig.xml内容如下:
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/demo" userId="root" password="123">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="model" targetProject="/home/virgil/IdeaProjects/myssm/src/main/java/com.virgil">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="/home/virgil/IdeaProjects/myssm/src/main/resources">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="/home/virgil/IdeaProjects/myssm/src/main/java/com.virgil">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
(4)直接双击idea的该出运行即可:
网上盗的图ubuntu下没装截图工具
在test目录下新建测试文件
testMybatis的内容如下:
import com.alibaba.fastjson.JSON;
import com.virgil.model.User;
import com.virgil.service.IUserService;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
/**
* Created by Administrator on 2017/10/14 0014.
*/
@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class testMybatis {
private static Logger logger = Logger.getLogger(testMybatis.class);
@Resource
private IUserService userService = null;
@Test
public void test1() {
User user = userService.selectUser(1);
logger.info(JSON.toJSONString(user));
}
}
IUserService.java
package com.virgil.service;
import com.virgil.model.User;
/**
* Created by Administrator on 2017/10/15 0015.
*/
public interface IUserService {
public User selectUser(int userId);
}
UserServiceImpl.java文件
package com.virgil.service.impl;
import com.virgil.dao.UserDao;
import com.virgil.model.User;
import com.virgil.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by Administrator on 2017/10/15 0015.
*/
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource
private UserDao userDao;
public User selectUser(int userId) {
return this.userDao.selectByPrimaryKey(userId);
}
}
index.jsp文件如下:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2017/10/15 0015
Time: 12:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>testtitle>
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/showUserById.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="selectUser()">onclick testbutton>
body>
html>
controller类如下:
package com.virgil.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.virgil.model.User;
import com.virgil.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Administrator on 2017/10/15 0015.
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/showUserById.do")
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
int userId = Integer.parseInt(request.getParameter("id"));
User user = this.userService.selectUser(userId);
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(user));
response.getWriter().close();
}
}
然后部署项目并启动………..over
首先你得有个github的帐号,没有就先注册
然后你得安装git,安装完成后打开gitbash
配置两项参数:
git config –global user.name “xxxxx”
git config –global user.email “xxxxxxxxxx”
接下来我们就可以直接用idea来完成所有的工作了
我们先用idea连上github,点击那个createAPIToken生成token
创建一个Create git Respository,选择你得项目
这是你得项目的所有文件颜色应该都还是红色的,这时我们选中项目,然后右键–git–add
这是所有的文件应该都变成绿色了
然后我们在commit directory
这是整个项目就提交到本地仓库了
最后我们需要push到github上面就可以了。