一创建新项目
二导入相关jar包
spring及springmvc的jar包
aspectjweaver.jar spring-aop-4.3.25.RELEASE.jar spring-aspects-4.3.25.RELEASE.jar spring-beans-4.3.25.RELEASE.jar spring-context-4.3.25.RELEASE.jar spring-context-support-4.3.25.RELEASE.jar spring-core-4.3.25.RELEASE.jar spring-expression-4.3.25.RELEASE.jar spring-jdbc-4.3.25.RELEASE.jar spring-orm-4.3.25.RELEASE.jar spring-tx-4.3.25.RELEASE.jar spring-web-4.3.25.RELEASE.jar spring-webmvc-4.3.25.RELEASE.jar
mybatis相关包
mybatis-3.5.2.jar ? ?//mybatis核心包 mybatis-spring-1.3.3.jar ?// ?spring 和 mybatis的整合包?
日志包 ?
commons-logging-1.2.jar log4j-1.2.17.jar log4j-api-2.11.2.jar log4j-core-2.11.2.jar
数据库包
mysql-connector-java-5.1.47.jar?
分页插件包
jsqlparser-2.1.jar pagehelper-5.1.10.jar?
连接池的包
druid-1.1.20.jar?
三数据库连接配置文件
jdbc.properties
#
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/1009-mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
#druid 内置监视器
jdbc.filters=stat,log4j,wall
# 最大连接数
jdbc.maxActive=20
# 最小连接数
jdbc.minIdle=1
日志的配置文件
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring配置文件
1.spring-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-tx.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>
2.spring-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1. 引入数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="FALLBACK"></context:property-placeholder>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="filters" value="${jdbc.filters}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
</bean>
<!-- 3.获取SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- mybatis相关配置信息 -->
<!-- 配置mapper映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="com.wxz.domain"></property>
<!-- 配置插件 -->
<property name="plugins">
<array><bean class="com.github.pagehelper.PageInterceptor"></bean></array>
</property>
</bean>
<!-- 4.使用SqlSessionFactory 生成mapper接口的代理类 -->
<!-- 使用mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxz.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
3.spring-tx.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置被管理的数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.spring-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- service层的包扫描 -->
<context:component-scan base-package="com.wxz.service.impl"></context:component-scan>
</beans>
springmvc配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="com.wxz.controller"></context:component-scan>
<!-- 开启mvc注解 -->
<!-- 开启了注解式的映射器和适配器 -->
<mvc:annotation-driven/>
</beans>
mybatis的mapper文件
<?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">
<mapper namespace="com.wxz.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.wxz.domain.User">
<!--@mbg.generated-->
<!--@Table `user`-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, `name`, `password`
</sql>
<select id="selectOne" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from `user`
where id = #{id}
</select>
<select id="selectList" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from `user`
</select>
<delete id="delete" parameterType="java.lang.Integer">
<!--@mbg.generated-->
delete from `user`
where id = #{id}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.wxz.domain.User" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into `user` (`name`, `password`)
values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
</insert>
<update id="update" parameterType="com.wxz.domain.User">
<!--@mbg.generated-->
update `user`
set `name` = #{name,jdbcType=VARCHAR},
`password` = #{password,jdbcType=VARCHAR}
where id = #{id}
</update>
</mapper>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!-- 配置listener 启动时 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!-- 配置springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>charsetFilter</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>charsetFilter</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
<!-- 配置druid监控页面 -->
<servlet>
<servlet-name>druid</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>druid</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
</web-app>
相关类
domain
package com.wxz.domain;
public class User {
private Integer id;
private String name;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public User() {
}
}
mapper
package com.wxz.mapper;
import com.wxz.domain.User;
import java.util.List;
public interface UserMapper {
int insert(User user);
int delete(Integer id);
User selectOne(Integer id);
List<User> selectList();
int update(User user);
}
service
package com.wxz.service;
import com.wxz.domain.User;
import java.util.List;
public interface UserService{
int insert(User user);
int delete(Integer id);
User selectOne(Integer id);
List<User> selectList();
int update(User user);
}
impl
package com.wxz.service.impl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wxz.mapper.UserMapper;
import com.wxz.domain.User;
import com.wxz.service.UserService;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
@Override
public int insert(User user) {
return userMapper.insert(user);
}
@Override
public int delete(Integer id) {
return userMapper.delete(id);
}
@Override
public User selectOne(Integer id) {
return userMapper.selectOne(id);
}
@Override
public List<User> selectList() {
return userMapper.selectList();
}
@Override
public int update(User user) {
return userMapper.update(user);
}
}
controller
package com.wxz.controller;
import com.wxz.domain.User;
import com.wxz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* TODO
*
* @author wxz
* @date 2022/2/23 22:35
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list.do")
public void queryList(){
List<User> users = userService.selectList();
System.out.println(users);
}
@RequestMapping("/user.do")
public void queryUser(Integer id){
User user = userService.selectOne(2);
System.out.println(user);
}
@RequestMapping("/add.do")
public void addUser(User user){
userService.insert(new User(1,"王一博","122323"));
System.out.println("新增成功");
}
@RequestMapping("/delete.do")
public void deleteUser(Integer id){
userService.delete(7);
System.out.println("删除成功");
}
@RequestMapping("/update.do")
public void updateUser(User user){
userService.update(new User(9,"赵丽颖","ttttt"));
System.out.println("修改成功");
}
}
至此,ssm整合成功,可以进行crud一套
使用@ResponseBody,标识返回的结果是一个json字符串
在springmvc中,springmvc默认是支持自动将对象转化为JSON字符串。且编码格式UTF-8.
但是springmvc默认支持json转化类,使用的jackson。需要主动的导入jackson的相关jar包。
jackson-annotations-2.9.9.jar jackson-core-2.9.9.jar jackson-databind-2.9.9.jar
?
|