Spring+Springmvc+Mybatis
SpringMVC 负责实现MVC设计模式,Mybatis负责数据持久层,
,Spring负责管理Spring mvc 和Mybatis相 关的对象创建和依赖注入.
-
创建Maven工程 ,pomxml
<dependencies> <!-- springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.15</version> </dependency> <!-- spring JDBC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.15</version> </dependency> <!--spring aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.15</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.15</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!--Mybatis 整合 spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> <!-- Mybatis sql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <!-- 数据连接池 C3mp0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- JSTL --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--ServletApl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>*.xml</include> <include>*.properties</include> </includes> </resource> </resources> </build>
-
web.xml 中配置 SpringMvc 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"> <!-- 启动spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC --> <servlet> <!-- 这里起名随意 --> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </init-param> </servlet> <!-- 拦截所用的请求 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符编码过滤器 --> <filter> <filter-name>chartEncoding</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>chartEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 加载静态资源 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> </web-app>
-
创建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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring.xml"></import> <import resource="springmvc.xml"></import> </beans>
在 spring.xml中配置Mybatis和 Spring整合
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 整合 mybatis --> <!-- 关联数据库的配置文件 --> <context:property-placeholder location="classpath:info.properties"/> <!--2.连接池 dbcp :半自动化操作 ,不能自动连接 c3p0 : 自动化操作 (自动化的加载配置文件,并且可以自动设置到对象中!) druid: hikari: --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <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> </bean> <!-- 配置 mybatis SqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 注入 绑定mybatis的配置文件 mybatis.xml --> <property name="mapperLocations" value=" classpath:mybatis-config.xml"></property> <property name="configLocation" value="classpath:config.xml"/> </bean> <!--配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中! --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <!--注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 扫描自定义的Mapper接口 --> <property name="basePackage" value="org.cs.*"/> </bean>
-
mybatis-config.xml 配置一些Mybatis的辅助信息,比如打印SQL等
<?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"> <configuration> <settings> <!--打印SQL --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <!-- 在指定一个包名 Mybatis 会在包名下去搜索需要的javaBean--> <package name="org.cs.entity"/> </typeAliases> <mappers> <mapper resource="org/cs/mapper/StudentInfoDaoMapper.xml"/> </mappers> </configuration>
-
配置 springmvc.xml
<?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 "> <!-- 启动注解驱动 --> <mvc:annotation-driven> </mvc:annotation-driven> <mvc:default-servlet-handler/> <!-- 扫描包 扫描业务代码 --> <context:component-scan base-package="org.cs.*"></context:component-scan> <!-- 配置事图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> </beans>
-
实体类org.sc.entity
import lombok.Data; @Data public class StudentInfo { private int id ; private String name ; private String sex ; private int age ; private String birthday ; private String address ; private int scid ; private ClassInfo classInfo ; }
-
接口org.sc.do
package org.cs.dao; import org.cs.entity.StudentInfo; import java.util.List; public interface StudentInfoDao { public List<StudentInfo> findAll(); }
-
org.sc.do.maper--StudentInfoDaoMapper.xml
<?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="org.cs.dao.StudentInfoDao"> <select id="findAll" resultType="StudentInfo"> SELECT * FROM student </select> </mapper>
org.cs.service---StudentService
package org.cs.service; import org.cs.entity.StudentInfo; import java.util.List; public interface StudentService { public List<StudentInfo> findAll(); }
-
org.cs.service.impl---StudentServiceImpl
package org.cs.service.impl; import org.cs.dao.StudentInfoDao; import org.cs.entity.StudentInfo; import org.cs.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentInfoDao studentInfoDao; @Override public List<StudentInfo> findAll(){ return studentInfoDao.findAll(); } public StudentServiceImpl(StudentInfoDao studentInfoDao) { this.studentInfoDao = studentInfoDao; } }
-
org.cs.controller---StudentHandler
package org.cs.controller; import org.cs.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/user") public class StudentHandler { @Autowired private StudentService studentService; @RequestMapping("/findAll") public ModelAndView findAll(){ ModelAndView mv = new ModelAndView(); mv.setViewName("index"); mv.addObject("list",studentService.findAll()); return mv; } }
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <a href="user/findAll" >SS</a> <c:forEach items="${list}" var="st"> ${st.id}--${st.name} </c:forEach> </body> </html>