Spring整合MyBatis

来源:互联网 发布:营销推广案例 知乎 编辑:程序博客网 时间:2024/06/06 03:52

Spring整合MyBatis

第一步:导入架包

<!-- Mysql jar包-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>
<!--  MyBatis jar包-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.2</version>
</dependency>
<!-- MyBatis-Spring jar包-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <!--Spring jar包-->
    
<groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
</dependency>

<!--事务-->
  
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.1.9.RELEASE</version>
  </dependency>

第二步:搭建分层

 接口:

查询所有信息

 

public interface userdao {
    public List<User> selectUserinfo()throws  Exception;

 

接口实现:public classuserdaoimp implements userdao {
    SqlSessionTemplate sqlSession;

    public SqlSessionTemplate getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession= sqlSession;
    }
    public List<User> selectUserinfo()throws Exception {
        return sqlSession.selectList("selectUserinfo");
    }
}

小配置:

<?xml version="1.0"encoding="ISO-8859-1"?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="cn.dao.userdao">
    <selectid="selectUserinfo"resultType="User">
        SELECT * from  user
    </select>
</mapper>

Entity省略

Service:

接口:

public interface userservice {
    public List<User> selectUserinfo()throws  Exception;
}

 

接口实现:

public class userserviceimp  implementsuserservice {
    userdao dao;

    public userdao getDao() {
        return dao;
    }

    public void setDao(userdao dao) {
        this.dao= dao;
    }

    public List<User> selectUserinfo()throws Exception {
        return dao.selectUserinfo();
    }

database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url
=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8
jdbc.username
=root
jdbc.password
=1234

小配置:

<?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>
    <typeAliases>
        <packagename="cn.entity"></package>
    </typeAliases>
</configuration>

大配置:

 

<!--1.dbcp数据源-->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
       <propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property>
       <propertyname="url"value="jdbc:mysql:///smbms"></property>
       <propertyname="username"value="root"></property>
       <propertyname="password"value="1234"></property>
</bean>
<!--2.配置SqlSessionFactoryBean-->
<beanid="SqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
       <propertyname="dataSource"ref="dataSource"></property>
       <propertyname="configLocation"value="classpath:mybatis-config.xml"></property>

</bean>


       <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <propertyname="basePackage"value="cn.dao"/>
              <propertyname="sqlSessionFactoryBeanName"value="SqlSessionFactory"/>
       </bean>
       <!--5.sevice注入dao-->
       
<beanid="userservice"class="cn.service.imp.userserviceimp">
              <propertyname="dao"ref="userdao"></property>
       </bean>

</beans>

单测: