目录
SpringCloud
SSM
Mybatis
MybatisUtil
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static{
//使用Mybatis获取sqlSessionFactory对象
try {
String resource="mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//SqlSessionFactory完全包含了面向数据库执行Sql命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configueration>
<!--引入外部配置 注意标签优先级,配置文件和标签都能写-->
<properties resource="db.properties">
<property name="password" value="123456"/>
</properties>
<!--设置控制台日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--数据库字段驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启全局缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--完全限定类更改别名-->
<typeAliases>
<package name="com.my.pojo"/>
</typeAliases>
<!--environments环境-->
<environments default="development">
<environment id="development">
<!--transactionManager事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<!--mysql8.0要配置时区-->
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
<mappers>
<package name=""/>
</mappers>
</configuration>
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username=root
maven过滤静态资源处理
<!--Maven滤设置 静态资源导出问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
Spring
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
//记得手动导入注解约束
<context:annotation-config/>
</beans>
Spring与Mybatis整合
SpringMVC
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">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Spring自带过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring-servlet.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"
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">
<context:component-scan base-package="com.my.controller"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
本文详细介绍了如何将SpringCloud与SSM(Spring、SpringMVC、Mybatis)进行整合,包括MybatisUtil工具类的创建、Mapper.xml与mybatis-config.xml的配置、db.properties数据库连接属性、Maven资源过滤处理以及Spring的applicationContext.xml配置。同时,还涉及了SpringMVC的web.xml与spring-servlet.xml配置,以及解决SpringMVC乱码问题的方法。
5549





