<!-- Spring -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-beans</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context-support</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
pojo层:
Items类
package com.test.pojo;
import java.util.Date;
public class Items {
private int id;
private String name;
private float price;
private String detail;
private String pic;
private Date createtime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", detail='" + detail + '\'' +
", pic='" + pic + '\'' +
", createtime=" + createtime +
'}';
}
}
dao层
接口 IItemsDao
package com.test.dao;
import com.test.pojo.Items;
import java.util.List;
public interface IItemsDao {
public List<items> selectItems();
}
</items>
实现类 ItemsDao
package com.test.dao.impl;
import com.test.dao.IItemsDao;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.List;
public class ItemsDao implements IItemsDao {
public ItemsDao()
{
}
public List<items> selectItems()
{
return null;
}
}
</items>
service层
接口:IItemsService
package com.test.service;
import com.test.pojo.Items;
import java.util.List;
public interface IItemsService {
public List<items> selectItems();
}
</items>
实现类:ItemsService
package com.test.service.impl;
import com.test.dao.IItemsDao;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
public class ItemsService implements IItemsService {
private IItemsDao itemsDao;
public List<items> selectItems()
{
return itemsDao.selectItems();
}
}
</items>
applicationContext-ioc.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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>
5.在配置文件中添加注解扫描包
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 设置注解 扫描 的包 -->
<context:component-scan base-package="com.test">
</context:component-scan></beans>
@Service
public class ItemsService implements IItemsService {
@Autowired
private IItemsDao itemsDao;
public List<items> selectItems()
{
return itemsDao.selectItems();
}
}
</items>
<!-- 导入mybatis需要的jar包 -->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.37</version>
</dependency>
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.4.6</version>
</dependency>
<dependency>
<groupid>com.mchange</groupid>
<artifactid>c3p0</artifactid>
<version>0.9.5.2</version>
</dependency>
<!-- 配置日志信息-->
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
<!-- spring 整合 mybatis-->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis-spring</artifactid>
<version>1.3.0</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<?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.test.mapper.ItemsMapper">
<select id="selectItems" resulttype="Items">
select * from items;
</select>
</mapper>
package com.test.mapper;
import com.test.pojo.Items;
import java.util.List;
public interface ItemsMapper {
public List<items> selectItems();
}
</items>
mybatis.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">
<configuration>
<!-- 设置日志的输出方法 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING">
</setting></settings>
<!-- 给pojo下面的类设置别名-->
<typealiases>
<package name="com.test.pojo">
</package></typealiases>
<mappers>
<mapper resource="com/test/mapper/ItemsMapper.xml">
</mapper></mappers>
</configuration>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
将之前在mybatis文件中配置的数据源 交给spring来管理,并创建sqlSessionFactory对象
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 设置注解 扫描 的包 -->
<context:component-scan base-package="com.test">
<!-- 导入db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 创建一个连接池对象 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}">
<property name="jdbcUrl" value="${jdbc.url}">
<property name="user" value="${jdbc.username}">
<property name="password" value="${jdbc.password}">
</property></property></property></property></bean>
<!-- 创建sqlSessionfactory对象-->
<bean id="sqlSessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource">
<property name="configLocation" value="classpath:mybatis.xml">
</property></property></bean>
</context:component-scan></beans>
package com.test.dao.impl;
import com.test.dao.IItemsDao;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.List;
@Component
public class ItemsDao implements IItemsDao {
@Autowired
private SqlSessionFactory sqlSessionFactory;
public ItemsDao()
{
}
public List<items> selectItems()
{
SqlSession sqlSession= sqlSessionFactory.openSession();
ItemsMapper itemsMapper= sqlSession.getMapper(ItemsMapper.class);
List<items> itemsList= itemsMapper.selectItems();
return itemsList;
}
}
</items></items>
在build标签中添加
<resources>
<resource>
<!-- 将Mapper的映射文件拷贝出来 -->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/applicationContext-ioc.xml");
IItemsService itemsService= classPathXmlApplicationContext.getBean("itemsService",IItemsService.class);
System.out.println(itemsService.selectItems());
}
1.在applicationContext-ioc.xml文件中 添加
<!-- 配置映射文件的扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper">
</property></bean>
@Autowired
private IItemsDao itemsDao;
修改为
@Autowired
private ItemsMapper itemsMapper;
完整代码如下
package com.test.service.impl;
import com.test.dao.IItemsDao;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemsService implements IItemsService {
@Autowired
private ItemsMapper itemsMapper;
public List<items> selectItems()
{
return itemsMapper.selectItems();
}
}
</items>
<!-- 导入SpringMvc 需要的jar包 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-web</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<?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:context="http://www.springframework.org/schema/context" 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/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">
<!-- 1. 扫描包 控制层 -->
<context:component-scan base-package="com.test.action">
<!-- 2. 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/">
<property name="suffix" value=".jsp">
</property></property></bean>
<!-- 3 开启注解-->
<mvc:annotation-driven></mvc:annotation-driven>
</context:component-scan></beans>
<!-- 配置 DispatcherServlet -->
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
让springmvc中的DispatcherServlet帮我们启动spring的相关配置文件
配置在DispatcherServlet的前面
<!-- 配置spring...xml文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-ioc.xml</param-value>
</context-param>
<!-- 配置context 加载的监听器 -->
<!-- 加载spring 的容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
package com.test.action;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/items")
public class ItemsAction {
@Autowired
private IItemsService itemsService;
public IItemsService getItemsService() {
return itemsService;
}
public void setItemsService(IItemsService itemsService) {
this.itemsService = itemsService;
}
@RequestMapping("/selectItemsList")
public ModelAndView selectItemsList()
{
//调用service的查询方法 返回一个list
List<items> itemsList= itemsService.selectItems();
//组装ModelAndView 对象
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList",itemsList);
modelAndView.setViewName("showItems");
//将数据存入 request,并转发到 WEB-INF/showItems.jsp页面
return modelAndView;
}
}
</items>
根据之前配置的试图解析器中的前缀,需要将jsp文件创建在 WEB-INF文件夹下
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- created by intellij idea. user: administrator date: 2019\6\26 0026 time: 10:36 to change this template use file | settings templates. --%>
<%@ page contenttype="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:foreach var="items" items="${itemsList}">
${items.name}
</c:foreach>
</body>
</html>
</%@></%--></%@>
<?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>com.test</groupid>
<artifactid>TestSSM</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>TestSSM Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-beans</artifactid>
<version>4.3.18.Release</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context-support</artifactid>
<version>4.3.18.Release</version>
</dependency>
<!-- 配置servlet-->
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>javax.servlet-api</artifactid>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--配置jsp的依赖 -->
<dependency>
<groupid>javax.servlet.jsp</groupid>
<artifactid>jsp-api</artifactid>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- 配置jstl -->
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>jstl</artifactid>
<version>1.2</version>
</dependency>
<dependency>
<groupid>taglibs</groupid>
<artifactid>standard</artifactid>
<version>1.1.2</version>
</dependency>
<!-- 导入mybatis需要的jar包 -->
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.37</version>
</dependency>
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.4.6</version>
</dependency>
<dependency>
<groupid>com.mchange</groupid>
<artifactid>c3p0</artifactid>
<version>0.9.5.2</version>
</dependency>
<!-- spring 整合 mybatis-->
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis-spring</artifactid>
<version>1.3.0</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- 配置日志信息-->
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
<!-- 导入SpringMvc 需要的jar包 -->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-web</artifactid>
<version>4.3.18.RELEASE</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<finalname>TestSSM</finalname>
<pluginmanagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactid>maven-clean-plugin</artifactid>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactid>maven-resources-plugin</artifactid>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactid>maven-compiler-plugin</artifactid>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactid>maven-surefire-plugin</artifactid>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactid>maven-install-plugin</artifactid>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactid>maven-deploy-plugin</artifactid>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginmanagement>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- 配置spring...xml文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-ioc.xml</param-value>
</context-param>
<!-- 配置context 加载的监听器 -->
<!-- 加载spring 的容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置 DispatcherServlet -->
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
db.user=root
db.password=123
<?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" xmlns:aop="http://www.springframework.org/schema/aop" 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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1. 配置扫描的包-->
<context:component-scan base-package="com.test">
<!-- 2. 导入db.properties -->
<context:property-placeholder location="classpath:db.properties">
<!-- 3 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driverClass}">
<property name="jdbcUrl" value="${db.url}">
<property name="user" value="${db.user}">
<property name="password" value="${db.password}">
</property></property></property></property></bean>
<!-- 4.创建sqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">
<property name="configLocation" value="classpath:mybatis.xml">
</property></property></bean>
<!-- 5.配置mapper接口的代理 需要扫描的包 -->
<!-- mapper.xml文件和ItemsMapper接口需要放在一个包中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper">
</property></bean>
</context:property-placeholder></context:component-scan></beans>
<?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:context="http://www.springframework.org/schema/context" 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/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">
<!-- 1. 扫描b包 控制层 -->
<context:component-scan base-package="com.test.action">
<!-- 2. 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/">
<property name="suffix" value=".jsp">
</property></property></bean>
<!-- 3 开启注解-->
<mvc:annotation-driven></mvc:annotation-driven>
</context:component-scan></beans>
Original: https://www.cnblogs.com/daimenglaoshi/p/16840186.html
Author: 呆萌老师
Title: ssm整合
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/714353/
转载文章受原作者版权保护。转载请注明原作者出处!