本文主要用于辅助本人使用ssm
maven选择
文件结构
pom
①spring-webmvc
mvc相关jar包
②jstl
jsp的一个依赖包
③commons-fileupload
文件上传的第三方服务
④commons-io
文件上传的io包依赖
⑤mysql-connector-java
mysql连接依赖
⑥Gson
第三方json依赖
⑦spring-jdbc
⑧c3p0
第三方数据库连接池依赖
⑨mybatis
mybatis依赖
⑩mybatis-spring
整合mybatis和spring的依赖
参考:
web
<?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">
<!--所有的用户请求都交给spring处理-->
<servlet-mapping>
<servlet-name>all</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>all</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化前端控制器的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/app.xml</param-value>
</init-param>
</servlet>
<!--指定默认访问地址-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--设置全局字符集-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
app
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--初始化contextClass-->
<context:component-scan base-package="com.txk.*"></context:component-scan>
<!--渲染视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--初始化mvc-->
<mvc:annotation-driven>
<!--设置全局字符集-->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--指定请求地址、文件地址-->
<mvc:resources mapping="/imags/**" location="/imags/"></mvc:resources>
<!--上传文件拦截 需要添加feiluplocd、commons io两个jar包-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--以下三个property可省-->
<property name="maxInMemorySize" value="10000000"></property>
<property name="maxUploadSize" value="10000000"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<import resource="db/spring-dao.xml"></import>
</beans>
spring-dao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
<!--导入属性文件 -->
<context:property-placeholder location="classpath:spring/db/db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--sqlsessionfactory的配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*Dao.xml"></property>
</bean>
<!--mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--dao 接口所对应的包-->
<property name="basePackage" value="com.txk.dao"></property>
</bean>
</beans>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://*****:3306/yvezhiluntang?useUnicode=true&characterEncoding=utf8
user=root
password=*****
注意点
①不需要
;
结尾
前面的属性要与spring-dao的数据源配置的value一致,数据源配置的key叫:driverClass、jdbcUrl不是driver,不是url!!
spring-dao的配置dao之后那么dao下面的*Dao.class都会被mvc管理不用写注解就会
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.txk.dao.UserDao">
<select id="finall" resultType="com.txk.bean.User">
select * from t_name
</select>
<!-- 方法名与id必须一致-->
<delete id="delete" parameterType="int">
delete * from t_name where id= #{id}
</delete>
<select id="findbyids" parameterType="list" resultType="com.txk.bean.User">
select * from t_name
<where>
id in(
<foreach collection="list" item="item" index="index" separator=",">#{item}</foreach>
)
</where>
</select>
<update id="update" parameterType="com.txk.bean.User" >
update t_name set name=#{name},password=#{password} where id=#{id}
</update>
<insert id="insert" parameterType="com.txk.bean.User">
insert into t_name values (null ,#{name},#{password})
</insert>
</mapper>
注意点
namespace 为dao接口文件的相对路径
标签要与sql语句相对应比如select 要使用
id是dao接口的方法名比如上面的我的UserDao是这样的
package com.txk.dao;
import com.txk.bean.User;
import java.util.List;
public interface UserDao {
List<User>finall();
void delete(int id);
User findbyids(List list);
void update(User user);
void insert(User user);
}
resultType:返回值类型,假如是集合则为集合中元素的类型(复合类型同理)
parameterType:传入参数类型(以下是类型对应)
传入参数复合类型的使用(上面列举了大部分情况)
Service使用dao
/**
* Copyright (C), 2021/9/23 11:04
*
* @author 田信坤
* Author: TianXinKun
* Date: 2021/9/23 11:04
*/
package com.txk.service;
import com.txk.bean.User;
import com.txk.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author TianXinKun
* @createTime 2021/9/23 11:04
* This program is protected by copyright laws.
* @version : 1.0
*/
@Service
public class UserService {
@Autowired
private UserDao userDao;
/*
* 提取所有user
* */
public List<User> finall(){
return userDao.finall();
}
/*
* 根据id删除
* */
public void delete(int id){
userDao.delete(id);
}
}
controller使用service
/**
* Copyright (C), 2021/9/23 11:03
*
* @author 田信坤
* Author: TianXinKun
* Date: 2021/9/23 11:03
*/
package com.txk.controller;
import com.txk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author TianXinKun
* @createTime 2021/9/23 11:03
* This program is protected by copyright laws.
* @version : 1.0
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "finall.do")
public void f(){
System.out.println(userService.finall());
}
@RequestMapping(value = "delete.do")
public void f1(int id){
userService.delete(id);
}
}