搭建ssm环境,各种xml配置
学完了ssm之后强烈建议大家去学习springboot,我当初也是学完ssm后看了几篇博客就开始使用springboot写项目。那是真的爽,根本不需要什么配置,开箱即用,想到当初搭建ssm环境时各种崩溃吐血,现在把一个简单的环境搭建分享出来。
这里写目录标题
搭建ssm环境,各种xml配置实体类Accountdao接口的注解开发service接口service实现类controller控制器方法用来测试的前端test.html(导入了jquery.min.js)视图解析器跳转位置success.html配置springmvc.xml配置applicationContext.xml添加jdbc.properties文件内容(数据库资源)在web.xml中添加前端控制器,监听器(扫描applicationContext.xml),格式转码添加pom.xml依赖ssm工程结构图
实体类Account
添加get和set方法,添加toString方法
private int id
;
private String name
;
private float money
;
dao接口的注解开发
用注解方式: @Repository(“accountDao”)实体注入 @Insert 插入数据 @Delete 删除数据 @Select 查询数据 @Update 更新数据
package cn
.com
.dao
;
import java
.util
.List
;
import org
.apache
.ibatis
.annotations
.Insert
;
import org
.apache
.ibatis
.annotations
.Select
;
import org
.springframework
.stereotype
.Repository
;
import cn
.com
.entity
.Account
;
@Repository("accountDao")
public interface IAccountDao {
@Select("select * from account1")
List
<Account> finall();
@Insert("insert into account1 (name,money) values(#{name},#{money})")
void addInfo(Account account
);
}
service接口
package cn
.com
.service
;
import java
.util
.List
;
import cn
.com
.entity
.Account
;
public interface IAccountService {
List
<Account> finall();
void addInfo(Account account
);
}
service实现类
package cn
.com
.service
.impl
;
import java
.util
.List
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
import cn
.com
.dao
.IAccountDao
;
import cn
.com
.entity
.Account
;
import cn
.com
.service
.IAccountService
;
@Service("accountService")
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao
;
@Override
public List
<Account> finall() {
System
.out
.println("service中的finall实现了......");
return accountDao
.finall();
}
@Override
public void addInfo(Account account
) {
accountDao
.addInfo(account
);
}
}
controller控制器方法
package cn
.com
.controller
;
import java
.util
.List
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.RequestBody
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
import cn
.com
.entity
.Account
;
import cn
.com
.service
.IAccountService
;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService
;
@RequestMapping("/findAll")
public String
findAll() {
System
.out
.println("controller执行了...");
List
<Account> accounts
= accountService
.finall();
for(Account account
: accounts
) {
System
.out
.println(account
);
}
return "success";
}
@RequestMapping("/addInfo")
@ResponseBody
public Account
addInfo(@RequestBody Map
<String,Object> map
,Account account
) throws Exception
{
System
.out
.println("addInfo执行了......");
BeanUtils
.populate(account
, map
);
accountService
.addInfo(account
);
return account
;
}
}
用来测试的前端test.html(导入了jquery.min.js)
<!DOCTYPE html
>
<html>
<head>
<meta charset
="UTF-8">
<script type
="text/javascript" src
="../js/jquery.min.js"></script
>
<title>Insert title here
</title
>
</head
>
<script type
="text/javascript">
function
run(){
var name
= $
("#name").val()
var money
= $
("#money").val()
var info
= {"name":name
,"money":money
};
console
.log(name
)
console
.log(money
)
$
.ajax({
type
:"post",
url
:"../account/addInfo",
data
:JSON
.stringify(info
),
contentType
: 'application/json; charset=UTF-8',
dataType
:"json",
success
: function(data
){
alert(data
.name
)
alert(data
.money
)
}
});
}
</script
>
<body>
<a href
="../account/findAll">查询所有信息
</a
><br>
name
:<input id
="name" name
="name"><br>
money
:<input id
="money" name
="money"><br>
<button onclick
="run()">ok
</button
>
</body
>
</html
>
视图解析器跳转位置success.html
<!DOCTYPE html
>
<html>
<head>
<meta charset
="UTF-8">
<title>Insert title here
</title
>
</head
>
<body>
<h1>success
</h1
>
</body
>
</html
>
配置springmvc.xml
<?xml version
="1.0" encoding
="UTF-8"?>
<beans xmlns
="http://www.springframework.org/schema/beans"
xmlns
:mvc
="http://www.springframework.org/schema/mvc" xmlns
:context
="http://www.springframework.org/schema/context"
xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi
:schemaLocation
="
http
://www
.springframework
.org
/schema
/beans
http
://www
.springframework
.org
/schema
/beans
/spring
-beans
.xsd
http
://www
.springframework
.org
/schema
/mvc
http
://www
.springframework
.org
/schema
/mvc
/spring
-mvc
.xsd
http
://www
.springframework
.org
/schema
/context
http
://www
.springframework
.org
/schema
/context
/spring
-context
.xsd"
>
<!--开启注解扫描,只扫描Controller注解
(要写到最终包下
)-->
<context
:component
-scan base
-package="cn.com.controller">
<!-- 只扫描Controller注解
-->
<context
:include
-filter type
="annotation" expression
="org.springframework.stereotype.Controller" />
</context
:component
-scan
>
<!--配置的视图解析器对象
-->
<bean id
="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name
="prefix" value
="/html/"/>
<property name
="suffix" value
=".html"/>
</bean
>
<!--过滤静态资源
-->
<mvc
:resources location
="/css/" mapping
="/css/**" />
<mvc
:resources location
="/images/" mapping
="/images/**" />
<mvc
:resources location
="/js/" mapping
="/js/**" />
<mvc
:resources location
="/html/" mapping
="/html/**" />
<!--开启SpringMVC注解的支持
-->
<mvc
:annotation
-driven
/>
</beans
>
配置applicationContext.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
:aop
="http://www.springframework.org/schema/aop"
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
/aop
http
://www
.springframework
.org
/schema
/aop
/spring
-aop
.xsd
http
://www
.springframework
.org
/schema
/tx
http
://www
.springframework
.org
/schema
/tx
/spring
-tx
.xsd"
>
<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理
-->
<context
:component
-scan base
-package="cn.com" >
<!--配置哪些注解不扫描
-->
<context
:exclude
-filter type
="annotation" expression
="org.springframework.stereotype.Controller" />
</context
:component
-scan
>
<!-- 配置整合mybatis过程
-->
<!-- 1.配置数据库相关参数properties的属性:$
{url
} -->
<context
:property
-placeholder location
="classpath:jdbc.properties" />
<!-- 2.数据库连接池
-->
<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}" />
<!-- c3p0连接池的私有属性
-->
<property name
="maxPoolSize" value
="20" />
<property name
="minPoolSize" value
="5" />
<!-- 关闭连接后不自动commit
-->
<property name
="autoCommitOnClose" value
="false" />
<!-- 获取连接超时时间
-->
<property name
="checkoutTimeout" value
="10000" />
<!-- 当获取连接失败重试次数
-->
<property name
="acquireRetryAttempts" value
="2" />
</bean
>
<!-- 3.配置SqlSessionFactory对象
-->
<bean id
="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池
-->
<property name
="dataSource" ref
="dataSource" />
</bean
>
<!-- 4.配置扫描Dao方法接口包,动态实现Dao接口,注入到spring容器中
-->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory
-->
<property name
="sqlSessionFactoryBeanName" ref
="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包
-->
<property name
="basePackage" value
="cn.com.dao" />
</bean
>
<!-- 配置事务管理器
-->
<bean id
="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池
-->
<property name
="dataSource" ref
="dataSource" />
</bean
>
<!-- 配置基于注解的声明式事务
-->
<tx
:annotation
-driven transaction
-manager
="transactionManager" />
</beans
>
添加jdbc.properties文件内容(数据库资源)
jdbc
.driver
=com
.mysql
.jdbc
.Driver
jdbc
.url
=jdbc
:mysql
://localhost
:3306/heima
?characterEncoding
=utf8
jdbc
.username
=root
jdbc
.password
=123456
在web.xml中添加前端控制器,监听器(扫描applicationContext.xml),格式转码
<?xml version
="1.0" encoding
="UTF-8"?>
<web
-app xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns
="http://java.sun.com/xml/ns/javaee" xsi
:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id
="WebApp_ID" version
="2.5">
<!--配置Spring的监听器,默认只加载WEB
-INF目录下的applicationContext
.xml配置文件
-->
<listener>
<listener
-class>org
.springframework
.web
.context
.ContextLoaderListener
</listener
-class>
</listener
>
<!--设置配置文件的路径
-->
<context
-param
>
<param
-name
>contextConfigLocation
</param
-name
>
<param
-value
>classpath
:applicationContext
.xml
</param
-value
>
</context
-param
>
<!--配置前端控制器
-->
<servlet>
<servlet
-name
>dispatcherServlet
</servlet
-name
>
<servlet
-class>org
.springframework
.web
.servlet
.DispatcherServlet
</servlet
-class>
<!--加载springmvc
.xml配置文件
-->
<init
-param
>
<param
-name
>contextConfigLocation
</param
-name
>
<param
-value
>classpath
:springmvc
.xml
</param
-value
>
</init
-param
>
<!--启动服务器,创建该servlet
-->
<load
-on
-startup
>1</load
-on
-startup
>
</servlet
>
<servlet
-mapping
>
<servlet
-name
>dispatcherServlet
</servlet
-name
>
<url
-pattern
>/</url
-pattern
>
</servlet
-mapping
>
<!--解决中文乱码的过滤器
-->
<filter>
<filter
-name
>characterEncodingFilter
</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
>characterEncodingFilter
</filter
-name
>
<url
-pattern
>
添加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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion
>
<groupId>com
.ssm
</groupId
>
<artifactId>ssm
</artifactId
>
<version>1.0</version
>
<packaging>war
</packaging
>
<name>ssm Maven Webapp
</name
>
<!-- FIXME change it to the project's website
-->
<url>http
://www
.example
.com
</url
>
<properties>
<project
.build
.sourceEncoding
>UTF
-8</project
.build
.sourceEncoding
>
<maven
.compiler
.source
>1.8</maven
.compiler
.source
>
<maven
.compiler
.target
>1.8</maven
.compiler
.target
>
<spring.version>5.0.2.RELEASE
</spring
.version
>
<slf4j.version>1.6.6</slf4j
.version
>
<log4j.version>1.2.12</log4j
.version
>
<mysql.version>5.1.6</mysql
.version
>
<mybatis.version>3.4.5</mybatis
.version
>
</properties
>
<dependencies>
<!-- spring
-->
<dependency>
<groupId>org
.aspectj
</groupId
>
<artifactId>aspectjweaver
</artifactId
>
<version>1.6.8</version
>
</dependency
>
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-aop
</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
-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
>
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-tx
</artifactId
>
<version>$
{spring
.version
}</version
>
</dependency
>
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-jdbc
</artifactId
>
<version>$
{spring
.version
}</version
>
</dependency
>
<dependency>
<groupId>junit
</groupId
>
<artifactId>junit
</artifactId
>
<version>4.12</version
>
<scope>compile
</scope
>
</dependency
>
<dependency>
<groupId>mysql
</groupId
>
<artifactId>mysql
-connector
-java
</artifactId
>
<version>$
{mysql
.version
}</version
>
</dependency
>
<dependency>
<groupId>javax
.servlet
</groupId
>
<artifactId>servlet
-api
</artifactId
>
<version>2.5</version
>
<scope>provided
</scope
>
</dependency
>
<dependency>
<groupId>javax
.servlet
.jsp
</groupId
>
<artifactId>jsp
-api
</artifactId
>
<version>2.0</version
>
<scope>provided
</scope
>
</dependency
>
<dependency>
<groupId>jstl
</groupId
>
<artifactId>jstl
</artifactId
>
<version>1.2</version
>
</dependency
>
<!-- log start
-->
<dependency>
<groupId>log4j
</groupId
>
<artifactId>log4j
</artifactId
>
<version>$
{log4j
.version
}</version
>
</dependency
>
<dependency>
<groupId>org
.slf4j
</groupId
>
<artifactId>slf4j
-api
</artifactId
>
<version>$
{slf4j
.version
}</version
>
</dependency
>
<dependency>
<groupId>org
.slf4j
</groupId
>
<artifactId>slf4j
-log4j12
</artifactId
>
<version>$
{slf4j
.version
}</version
>
</dependency
>
<!-- log end
-->
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
</artifactId
>
<version>$
{mybatis
.version
}</version
>
</dependency
>
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
-spring
</artifactId
>
<version>1.3.0</version
>
</dependency
>
<dependency>
<groupId>c3p0
</groupId
>
<artifactId>c3p0
</artifactId
>
<version>0.9.1.2</version
>
<type>jar
</type
>
<scope>compile
</scope
>
</dependency
>
<!--json数据格式转换
-->
<dependency>
<groupId>com
.fasterxml
.jackson
.core
</groupId
>
<artifactId>jackson
-databind
</artifactId
>
<version>2.9.0</version
>
</dependency
>
<dependency>
<groupId>com
.fasterxml
.jackson
.core
</groupId
>
<artifactId>jackson
-core
</artifactId
>
<version>2.9.0</version
>
</dependency
>
<dependency>
<groupId>com
.fasterxml
.jackson
.core
</groupId
>
<artifactId>jackson
-annotations
</artifactId
>
<version>2.9.0</version
>
</dependency
>
<!-- JSONObject对象依赖的jar包
-->
<dependency>
<groupId>commons
-beanutils
</groupId
>
<artifactId>commons
-beanutils
</artifactId
>
<version>1.9.3</version
>
</dependency
>
<dependency>
<groupId>commons
-collections
</groupId
>
<artifactId>commons
-collections
</artifactId
>
<version>3.2.1</version
>
</dependency
>
<dependency>
<groupId>commons
-lang
</groupId
>
<artifactId>commons
-lang
</artifactId
>
<version>2.6</version
>
</dependency
>
<dependency>
<groupId>commons
-logging
</groupId
>
<artifactId>commons
-logging
</artifactId
>
<version>1.1.1</version
>
</dependency
>
<dependency>
<groupId>net
.sf
.ezmorph
</groupId
>
<artifactId>ezmorph
</artifactId
>
<version>1.0.6</version
>
</dependency
>
<dependency>
<groupId>net
.sf
.json
-lib
</groupId
>
<artifactId>json
-lib
</artifactId
>
<version>2.2.3</version
>
<classifier>jdk15
</classifier
><!-- 指定jdk版本
-->
</dependency
>
</dependencies
>
</dependencies
>
<build>
<plugins>
<plugin>
<groupId>org
.apache
.maven
.plugins
</groupId
>
<artifactId>maven
-compiler
-plugin
</artifactId
>
<version>3.1</version
>
<configuration>
<source>1.8</source
>
<target>1.8</target
>
</configuration
>
</plugin
>
</plugins
>
</build
>
</project
>
ssm工程结构图
搭建的环境的时候还有个很大的问题,就是maven导入的jar包版本不兼容,当初为了这事折腾了好久,下次出一期关于maven和jar包依赖的文章。