首先:
需要配置的是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>SSMTest</artifactId>
<version>1.0-SNAPSHOT</version>
<!--Spring context-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependencies>
<!--spring context-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--Spring aop-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--aspect-->
<!--Spring web mvc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--spring webmvc mvc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--spring test 单元测试-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
<!--spring tx (spring transaction) 事务-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--MysqL connector java 8.0.12-->
<!-- https://mvnrepository.com/artifact/MysqL/mysql-connector-java -->
<dependency>
<groupId>MysqL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!--spring jdbc 是jdbc模板技术-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--servlet api-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--jstl-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--log start-->
<!--log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!-- <dependency>-->
<!-- <groupId>log4j</groupId>-->
<!-- <artifactId>log4j</artifactId>-->
<!-- <version>1.2.17</version>-->
<!-- </dependency>-->
<!-- <!–slf4j–>-->
<!-- <!– https://mvnrepository.com/artifact/org.slf4j/slf4j-api –>-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-api</artifactId>-->
<!-- <version>1.7.25</version>-->
<!-- <scope>compile</scope>-->
<!-- </dependency>-->
<!--log end-->
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mybatis - spring-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--c3p0-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!--报错家的包-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-api</artifactId>-->
<!-- <version>1.5.6</version>-->
<!-- <type>jar</type>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>slf4j-simple</groupId>-->
<!-- <artifactId>slf4j-simple</artifactId>-->
<!-- <version>1.5.2</version>-->
<!-- </dependency>-->
</dependencies>
</project>
项目结构:
一:Spring
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--开启注解扫描,希望处理service & dao,不希望Spring框架去处理-->
<context:component-scan base-package="com">
<!--配置一些不需要扫描的包-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
测试类:
class AccountServiceImpltest {
@Test
public void test(){
//加载配置文件
// FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("/Webroot/WEB-INF/applicationContext.xml");
ApplicationContext context = new ClasspathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象:accountService是在Impl上声明的注解对象,但其实可以继承为父类接口的对象
AccountService accountService = context.getBean("accountService", AccountService.class);
//调用方法
accountService.findAll();
}
}
二: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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置SpringMVC的一步:前端控制器-->
<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>
<!--3.设置中文乱码的问题-->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
配置springmbc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.开启注解扫描,只扫描Controller注解-->
<!--SpringMVC配置文件-->
<context:component-scan base-package="com">
<!--spring不扫描controller,MVC就得扫描,分工明确-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--2.配置的视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--后-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--3.过滤静态资源-->
<mvc:resources mapping="/css/" location="/css/**"></mvc:resources>
<mvc:resources mapping="/images/" location="/images/**"></mvc:resources>
<mvc:resources mapping="/js/" location="/js/**"></mvc:resources>
<!--4.开启SpringMVC注解支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
测试类
package com.young.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Create by wuyang9769@163.com
* Decorate:
* web controller
* @author young_wu
* @date 2020/11/23 0023 16:17
* You can't be killed, it will only make you stronger
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("/test")
public String test(){
System.out.println("控制器调转!");
return "success";
}
}
三:Spring & SpringMVC
思路是将业务层的对象和控制层的对象放在容器里,
方式使用DI,
那么就要就要加载Spring的配置文件
配置web.xml用监听器来加载Spring的配置文件
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--三:整合Spring & SpringMVC 利用监听器,加载默认为: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>
<!--配置SpringMVC的一步:前端控制器-->
<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>
<!--3.设置中文乱码的问题-->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
测试类:
@Controller
@RequestMapping("/account")
public class AccountController {
/*依赖注入*/
@Autowired
private AccountService accountService;
/*视图解析*/
@RequestMapping("/test")
public String test(){
System.out.println("控制层方法实现");
accountService.findAll();
return "success";
}
}
四:MyBatis
配置mybatis的环境
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dta//mybatis-3-config.dtd">
<configuration>
<!--1.配置环境-->
<environments default="MysqL">
<environment id="MysqL">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.MysqL.cj.jdbc.Driver"/>
<property name="url" value="jdbc:MysqL://127.0.0.1/girls?& useSSL=false& serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<!--2.引入配置文件-->
<mappers>
<!--写法一:-->
<!-- <mapper resource="com/young/dao/xx.xml"></mapper>-->
<!--写法二:扫描报下所有文件-->
<package name="com.young.dao"/>
<!--写法三:只扫描AccountDao-->
<!-- <mapper class="com.young.dao.AccountDao"></mapper>-->
</mappers>
</configuration>
Dao的注解:
public interface AccountDao {
//查询所有账户
@Select("select * from admin")
public List<Account> findAll();
//存储用户信息
@Insert("insert into admin (id,username,password) values(#{id},#{username},#{password})")
public void saveAccount(Account account);
}
测试类:
package com.young.dao;
import com.young.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.sqlSession;
import org.apache.ibatis.session.sqlSessionFactory;
import org.apache.ibatis.session.sqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Create by wuyang9769@163.com
* Decorate:
*
* @author young_wu
* @date 2020/11/23 0023 19:27
* You can't be killed, it will only make you stronger
*/
class AccountDaoTest {
/*测试查询*/
@Test
public void test() throws IOException {
/*1.加载配置文件*/
InputStream is = Resources.getResourceAsstream("sqlMapConfig.xml");
/*2.创建sqlSessionFactory对象*/
sqlSessionFactory factory= new sqlSessionFactoryBuilder().build(is);
/*3.创建sqlSession对象*/
sqlSession session = factory.openSession();
/*4.获取代理对象*/
AccountDao mapper = session.getMapper(AccountDao.class);
/*查询*/
List<Account> list = mapper.findAll();
for(Account account : list){
System.out.println(account);
}
/*5.关闭资源*/
is.close();
session.close();
}
/*测试save*/
@Test
public void test1() throws IOException {
Account account = new Account();
account.setId(77);
account.setUsername("fuck");
account.setPassword("qwqwq");
/*1.加载配置文件*/
InputStream is = Resources.getResourceAsstream("sqlMapConfig.xml");
/*2.创建sqlSessionFactory对象*/
sqlSessionFactory factory= new sqlSessionFactoryBuilder().build(is);
/*3.创建sqlSession对象*/
sqlSession session = factory.openSession();
/*4.获取代理对象*/
AccountDao mapper = session.getMapper(AccountDao.class);
//这一地步没有用,没有提交事务
mapper.saveAccount(account);
//提交事务
session.commit();
/*5.关闭资源*/
is.close();
session.close();
}
}
五:整合SpringMVC & MyBatis
编写配置文件web.xml:
<!--1.Spring整合MyBatis框架-->
<!--2.配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.MysqL.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:MysqL://127.0.0.1/girls?& useSSL=false& serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!--3.配置sqlSessionFactory工厂-->
<bean id="factory" class="org.mybatis.spring.sqlSessionfactorybean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.配置AccountDao接口所在包-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.young.dao"></property>
</bean>
</beans>
测试前:在AccoutDao加注解@Repository,且将sqlMapConfig.xml
测试类:
@Controller
@RequestMapping("/account")
public class AccountController {
/*依赖注入*/
@Autowired
private AccountService accountService;
/*视图解析*/
@RequestMapping("/test")
public String test(Model model){
System.out.println("表现层方法实现");
//调用service方法
List<Account> list = accountService.findAll();
model.addAttribute("list",list);
return "success";
}
}
JSP:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
<a href="account/test">this is index.jsp</a>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
<%--引入JSTL标签库便利list--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
</head>
<body>
<h1>this is success!</h1>
<c:forEach items="${list}" var="account">
${account.username}
</c:forEach>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。