案例一
包含dao层
创建maven webapp项目
maven仓库需要改为阿里云
引入依赖
<?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>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>
<!-- 1.导入Spring相关的jar包 -->
<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>
<!-- 导入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>
</dependencies>
<build>
<resources>
<resource>
<!-- 将Mapper的映射文件拷贝出来 -->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</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>
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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!-- 1.扫描包中的注解 -->
<context:component-scan base-package="com.test" />
<!--导入db.properties文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--创建了数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>
<!--创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
<!-- 扫描mapper文件 生成代理对象 替代之前dao层的操作 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper" />
</bean>
</beans>
数据库信息
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/itstar
user=itstar
password=yyy123456
mybatis配置文件
<?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>
<!--配置db.properties 转移到spring中-->
<!--<properties resource="db.properties" />-->
<!-- 配置日志管理 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 设置别名-->
<typeAliases>
<package name="com.test.pojo" />
</typeAliases>
<!-- 转移到spring配置文件中 -->
<!--<environments default="development">-->
<!--<environment id="development">-->
<!--<transactionManager type="JDBC"/>-->
<!--<dataSource type="POOLED">-->
<!--<property name="driver" value="${driverClass}"/>-->
<!--<property name="url" value="${url}"/>-->
<!--<property name="username" value="${user}"/>-->
<!--<property name="password" value="${password}"/>-->
<!--</dataSource>-->
<!--</environment>-->
<!--</environments>-->
<!--注册mapper文件 转移到spring中-->
<!--<mappers>-->
<!--<package name="com.test.mapper" />-->
<!--</mappers>-->
</configuration>
dao层
package com.test.dao;
import com.test.pojo.Items;
import java.util.List;
public interface IItemsDao {
public List<Items> selectItems();
}
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
//实现类
@Component
public class ItemsDao implements IItemsDao {
@Autowired
private SqlSessionFactory sqlSessionFactory;
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
//转移到Spring的配置文件中 让Spring创建SqlSessionFactory对象
// public ItemsDao()
// {
// InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("sqlMapConfig.xml");
//
// SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
//
// this.sqlSessionFactory= sqlSessionFactoryBuilder.build(inputStream);
// }
//查询所有的商品信息
@Override
public List<Items> selectItems() {
SqlSession sqlSession= sqlSessionFactory.openSession();
ItemsMapper itemsMapper= sqlSession.getMapper(ItemsMapper.class);
return itemsMapper.selectItems();
}
}
mapper
package com.test.mapper;
import com.test.pojo.Items;
import java.util.List;
//创建一个接口 代理ItemsMapper.xml文件
public interface ItemsMapper {
public List<Items> selectItems();
}
<?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>
service层
package com.test.service;
import com.test.pojo.Items;
import java.util.List;
public interface IItemsService {
public List<Items> selectItems();
}
package com.test.service.impl;
import com.test.dao.IItemsDao;
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;
//service层的实现类
@Service
public class ItemsService implements IItemsService {
@Autowired
private IItemsDao itemsDao;
public IItemsDao getItemsDao() {
return itemsDao;
}
public void setItemsDao(IItemsDao itemsDao) {
this.itemsDao = itemsDao;
}
@Override
public List<Items> selectItems() {
return itemsDao.selectItems();
}
}
测试类
package com.test.service;
import com.test.pojo.Items;
import com.test.service.impl.ItemsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestItemsService {
@Test
public void test()
{
//测试service层的查询方法
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
IItemsService itemsService= applicationContext.getBean("itemsService",IItemsService.class);
List<Items> itemsList= itemsService.selectItems();
System.out.println(itemsList);
}
}
案例二
省略dao层,由spring代替
在案例一的基础上,修改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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!-- 1.扫描包中的注解 -->
<context:component-scan base-package="com.test" />
<!--导入db.properties文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--创建了数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>
<!--创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
<!-- 扫描mapper文件 生成代理对象 替代之前dao层的操作 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper" />
</bean>
</beans>
删除dao层
修改service层
package com.test.service.impl;
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;
//service层的实现类
@Service
public class ItemsService implements IItemsService {
//spring会帮我们创建一个代理对象( 实现了ItemsMapper接口的),代理对象代理的是dao层的功能
@Autowired
private ItemsMapper itemsMapper;
public ItemsMapper getItemsMapper() {
return itemsMapper;
}
public void setItemsMapper(ItemsMapper itemsMapper) {
this.itemsMapper = itemsMapper;
}
@Override
public List<Items> selectItems() {
return itemsMapper.selectItems();
}
}