包含dao层
maven仓库需要改为阿里云
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>testSSMartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>testSSM Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.18.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.18.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.18.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.3.18.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.37version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.18.RELEASEversion>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<finalName>testSSMfinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
<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">
<context:component-scan base-package="com.test" />
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
bean>
<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
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"/>
settings>
<typeAliases>
<package name="com.test.pojo" />
typeAliases>
configuration>
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();
}
}
package com.test.mapper;
import com.test.pojo.Items;
import java.util.List;
//创建一个接口 代理ItemsMapper.xml文件
public interface ItemsMapper {
public List<Items> selectItems();
}
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.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代替
<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">
<context:component-scan base-package="com.test" />
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper" />
bean>
beans>
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();
}
}