Spring和MyBatis的整合的查询小案例

来源:互联网 发布:编写c语言的步骤 编辑:程序博客网 时间:2024/05/29 12:14

一、jar包的导入




pom.xml


 <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis-spring</artifactId>      <version>1.2.0</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>3.2.13.RELEASE</version>    </dependency>    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.31</version>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis</artifactId>      <version>3.2.2</version>    </dependency>    <dependency>      <groupId>aopalliance</groupId>      <artifactId>aopalliance</artifactId>      <version>1.0</version>    </dependency>    <dependency>      <groupId>org.aspectj</groupId>      <artifactId>aspectjweaver</artifactId>      <version>1.6.9</version>    </dependency>    <dependency>      <groupId>log4j</groupId>      <artifactId>log4j</artifactId>      <version>1.2.17</version>    </dependency>    <dependency>      <groupId>commons-dbcp</groupId>      <artifactId>commons-dbcp</artifactId>      <version>1.4</version>    </dependency>    <dependency>      <groupId>commons-pool</groupId>      <artifactId>commons-pool</artifactId>      <version>1.6</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>RELEASE</version>    </dependency>  </dependencies>    <build>    <resources>      <resource>        <directory>src/main/java</directory>        <includes>          <include>**/*.xml</include>        </includes>      </resource>    </resources>  </build>

二、建立分层




三、配置mybatis-config.xml

<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <!--别名-->        <typeAliases>        <package name="cn.entity"/>    </typeAliases></configuration>


1.实体类Dept


public class Dept {    private int id;//部门编号    private String name;//部门名称    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;    }}

2.Dao层  DeptDao


public interface DeptDao {    //获得所有部门    public List<Dept> getAllDept();}

3.Dao 层 映射文件 DeptDao.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="cn.dao.DeptDao">    <select id="getAllDept" resultType="Dept">        SELECT * FROM department    </select></mapper>


4. Dao 层表现层impl  DeptDaoImpl

public class DeptDaoImpl implements DeptDao {    SqlSessionTemplate sqlSession;    public SqlSessionTemplate getSqlSession() {        return sqlSession;    }    public void setSqlSession(SqlSessionTemplate sqlSession) {        this.sqlSession = sqlSession;    }    public List<Dept> getAllDept() {        return sqlSession.selectList("getAllDept");    }}

5.service层  DeptService

public interface DeptService {    //获得所有部门    public List<Dept> getAllDept();}

6.service层表现层impl  DeptServiceImpl

public class DeptServiceImpl implements DeptService {    DeptDao dao;    public DeptDao getDao() {        return dao;    }    public void setDao(DeptDao dao) {        this.dao = dao;    }    //获得所有部门    public List<Dept> getAllDept() {        return dao.getAllDept();    }}

四、配置applicationContext.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:p="http://www.springframework.org/schema/p"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:aop="http://www.springframework.org/schema/aop"           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/aop         http://www.springframework.org/schema/aop/spring-aop.xsd">    <!--1.dbcp数据源-->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>        <property name="url" value="jdbc:mysql:///empdb"></property>        <property name="username" value="root"></property>        <property name="password" value=""></property>    </bean>    <!--2.配置SqlSessionFactoryBean-->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        <!--映射文件-->        <property name="mapperLocations" >            <list>                <value>classpath:cn/dao/*.xml</value>            </list>        </property>    </bean>    <!--3.配置SqlSessionTemplate-->   <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>   </bean>    <!--4.dao注入模板-->    <bean id="deptDao" class="cn.dao.impl.DeptDaoImpl">    <property name="sqlSession" ref="sqlSession"></property>    </bean>    <!--5.sevice注入dao-->    <bean id="deptService" class="cn.service.impl.DeptServiceImpl">        <property name="dao" ref="deptDao"></property>    </bean></beans>


五、单测  test层下的MyTest类

public class MyTest {    @Test    public void getAllDeptTest(){        ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");        DeptService service = (DeptService)cxt.getBean("deptService");        List<Dept> list = service.getAllDept();        for(Dept item:list){            System.out.println(item.getName());        }    }}


原创粉丝点击