spring项目中target项目是什么‘’_手把手教你搭建一个SSM项目

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:40   1880   0

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。

下面手把手教你使用IDEA搭建一个SSM项目:

新建project

File—》新建project,使用maven作为包管理工具,因为我这里使用了父项目,所以我是新建一个module,都一样的。

025437aea97b852a47fc8791caef96f9.png

设置名称:

8653256b9a14c684f7c58d104663254f.png

我们打开发现,项目没有java目录,没关系我们可以自己创建,打开文件结构(快捷键 Ctrl+Shift+Alt+S)

b859f4a35f22b8b228037172e4d45570.png

然后把这个java文件夹设置为source目录:

8187af7ac84ab3cfd08b744124e7f1bc.png

如果application.xml表头的xml报错,可以把链接复制过来:

该图片来源见水印

425dc913f1cb96e905ab8abb1c5fc821.png
错误例子
e278622b12808a2f69978c81bd58540d.png

1 引入spring核心组件

修改pom.xml , 注意标签

    <properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

<spring.version>4.3.5.RELEASEspring.version>

<mybatis.version>3.4.1mybatis.version>
properties>

<dependencies>








<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>


<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>

<version>1.3.3version>
dependency>




<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>


<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>

<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.9version>
dependency>


<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.35version>
dependency>
dependencies>


<build>
<finalName>SSM_projectfinalName>
<plugins>
<plugin>

<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.6.1version>
<configuration>
<source>${maven.compiler.source}source>
<target>${maven.compiler.target}target>
<encoding>UTF8encoding>
configuration>
plugin>
plugins>
build>

2 配置web.xml

这里引入了两个文件 applicationContext.xmlspring-mvc.xml

<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>mvcDemodisplay-name>

<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>


<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>


<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>

<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-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-mvc.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>

web-app>

注意:以下配置文件均在resources 目录下

3 新建`log4j.properties`

配置:

#日志输出级别
log4j.rootLogger=debug,stdout,D,E

#设置stdout的日志输出控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#输出日志到控制台的方式,默认为System.out
log4j.appender.stdout.Target = System.out
#设置使用灵活布局
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#灵活定义输出格式
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} -[%p] method:[%c (%rms)] - %m%n

如果不需要日志管理,可以不配置,类似于log4j的日志管理工具还有logback,

logback可以参考:https://blog.csdn.net/yudianxiaoxiao/article/details/86664616

4 新建 `jdbc.properties`

本地数据库配置:

jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=root
#密码
jdbc.password=root
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2

初始化一个表:

DROP TABLE IF EXISTS t_user_hellocoder;
CREATE TABLE `t_user_hellocoder` (
`id` int(11) NOT NULL,
`name` VARCHAR(11) DEFAULT NULL,
`description` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

INSERT INTO `test`.`t_user_hellocoder`(`id`, `name`, `description`) VALUES (1, ''我是HaC'', ''一枚混迹于互联网的程序猿'');

5 配置 `applicati onContext.xml` 和 `spring-mvc.xml`

新建 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">


<context:component-scan base-package="com.ssm.hellocoder"/>


<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/jsp/"/>

<property name="suffix" value=".jsp"/>
bean>
beans>

applicationContext.xml:

我新建的项目applicationContext.xmlWEB-INF目录下,需要把它移动到resources目录下,如果本来就在就不用管,没有就新建。

<?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:tx="http://www.springframework.org/schema/tx" 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.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
bean>


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>

<property name="typeAliasesPackage" value="com.ssm.hellocoder.entity"/>

<property name="mapperLocations" value="classpath:mapper/TUserHellocoderDao.xml"/>
bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

<property name="basePackage" value="com.ssm.hellocoder.dao"/>
bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>
bean>
beans>

6 新建controller、service、dao、entity、xml、jsp

先上目录结构,大家不懂的可以仿照一下:

fb7eda0819c652a9fd0be7730d00722f.png

TUserHellocoder.java

package com.ssm.hellocoder.entity;

import java.io.Serializable;

/**
* (TUserHellocoder)实体类
*
* @author HaC
* @since 2020-05-29 01:28:27
*/
public class TUserHellocoder implements Serializable {
private static final long serialVersionUID = 417502814533585676L;

private Integer id;

private String name;

private String description;


public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

TUserHellocoderController.java

package com.ssm.hellocoder.controller;

import com.ssm.hellocoder.service.TUserHellocoderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
* (TUserHellocoder)表控制层
*
* @author HaC
* @since 2020-05-29 00:47:08
*/
@Controller
@RequestMapping("/HelloCoder")
public class TUserHellocoderController {
/**
* 服务对象
*/
@Resource
private TUserHellocoderService tUserHellocoderService;

/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("/selectUser")
public ModelAndView selectOne(@RequestParam(value = "id") Integer id) {
ModelAndView md = new ModelAndView("index");
//把数据存到ModelAndView,给前端
md.addObject("user",this.tUserHellocoderService.queryById(id));
return md;
}
}

TUserHellocoderService.java

package com.ssm.hellocoder.service;

import com.ssm.hellocoder.entity.TUserHellocoder;

/**
* (TUserHellocoder)表服务接口
*
* @author HaC
* @since 2020-05-29 01:28:29
*/
public interface TUserHellocoderService {

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TUserHellocoder queryById(Integer id);

}

TUserHellocoderServiceImpl.java

package com.ssm.hellocoder.service.impl;

import com.ssm.hellocoder.dao.TUserHellocoderDao;
import com.ssm.hellocoder.entity.TUserHellocoder;
import com.ssm.hellocoder.service.TUserHellocoderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* (TUserHellocoder)表服务实现类
*
* @author HaC
* @since 2020-05-29 01:28:29
*/
@Service("tUserHellocoderService")
public class TUserHellocoderServiceImpl implements TUserHellocoderService {
@Resource
private TUserHellocoderDao tUserHellocoderDao;

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public TUserHellocoder queryById(Integer id) {
return this.tUserHellocoderDao.queryById(id);
}
}

TUserHellocoderDao.java

package com.ssm.hellocoder.dao;

import com.ssm.hellocoder.entity.TUserHellocoder;

/**
* (TUserHellocoder)表数据库访问层
*
* @author HaC
* @since 2020-05-29 01:28:28
*/
public interface TUserHellocoderDao {

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TUserHellocoder queryById(Integer id);

}

resources目录下新建一个文件夹mapper,新建文件 TUserHellocoderDao.xml

<?xml  version="1.0" encoding="UTF-8"?>
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.hellocoder.dao.TUserHellocoderDao">

<resultMap type="com.ssm.hellocoder.entity.TUserHellocoder" id="TUserHellocoderMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="description" column="description" jdbcType="VARCHAR"/>
resultMap>


<select id="queryById" resultMap="TUserHellocoderMap">
select
id, name, description
from test.t_user_hellocoder
where id = #{id}
select>

mapper>

WEB-INF目录下新建一个文件夹jsp,新建文件 index.jsp



Hello World!


${user.name}


${user.description}




以上就是SSM项目的配置了。

7 运行

配置服务器运行:

加上 -Dfile.encoding=UTF-8 解决日志乱码

85c29429437578fe78a1af615eaab880.png

选择war包:

155e3796dd8b27c8bdc3af404507db76.png

运行

浏览器输入 http://localhost:8081/HelloCoder/selectUser?id=1

a23aaa81a1ec594926d18f8868fd9616.png

大功告成!

源码在Github:https://github.com/DogerRain/DemoFamily

--end--

7e6ac97614dbca7daf0105d94c46c53e.png
长按关注噢! 万水千山总是情,点个在看行不行

4a88fa9182fb75198dbf9123cba98ebe.gif

d6d7a72f5cdf7c76fa62254a30904d61.png

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP