首页 > 编程语言 > 详细

Spring整合Mybatis

时间:2020-08-01 22:21:45      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:ins   实现   setname   unit   names   bean   

整合Mybatis

步骤:

  1. 导入相关jar包
    • junit
    • mybatis
    • mysql数据库连接
    • spring相关包
    • aop织入
    • mybatis-spring
  2. 编写配置文件
  3. 测试

1、导入相关jar包

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>mysq1</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>
<!--Spring操作数据库的话,还需要一个spring-jdbc-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

2、回忆Mybatis

  1. 编写实体类
  2. 编写核心配置文件
  3. 编写接口
  4. 编写Mapper.xml
  5. 测试

3、Mybatis-Spring

1、导入相关jar包

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

2、创建mybatis-config.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>
    
  <typeAliases>
      <package name="com.qilu.bean"/>
  </typeAliases>

</configuration>

3、创建spring-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

	<!--DataSource:使用Spring的数据源提供的Mybatis的配置 c3p0 dbcp druid
           我们这里使用Spring提供的JDBC
	-->
	<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
    	<property name="username" value="root"/>
	    <property name="password" value="123456"/>
	</bean>
    
        <!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource" ref="dataSource" />
            <!-- 绑定Mybatis配置文件 -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
	    <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/ >
	</bean>
        
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能使用构造器注入,因为他没有构造方法 -->
	    <constructor-arg index="0" ref="sqlSessionFactory" />
        </bean>
</beans>

4、编写代码

1、编写bean类

public class User {
    private int id;
    private String name;
    private String password;
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ‘}‘;
    }
}

2、编写UserMapper接口

public interface UserMapper {
    public List<User> selectUser();
}

3、编写UserMapper.xml配置文件

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.qilu.mapper.UserMapper">
    <select id="selectUser" resultType="com.qilu.bean.User">
    	select * from user;
    </select>
</mapper>

4、编写UserMapperImpl实现类

public class UserMapperImpl implements UserMapper {
    //我们的所有操作,原来都由sqlSession来执行,现在都由SqlSessionTemplate来完成
    private SqlsessionTemplate sqlSession;

    public void setSq1Session(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
        public List<User> selectUser () {
            UserMapper mapper = sq1Session.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
}

5、注入spring-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

	<!--DataSource:使用Spring的数据源提供的Mybatis的配置 c3p0 dbcp druid
           我们这里使用Spring提供的JDBC
	-->
	<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
	    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>
    	    <property name="username" value="root"/>
	    <property name="password" value="123456"/>
	</bean>
    
        <!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource" ref="dataSource" />
            <!-- 绑定Mybatis配置文件 -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
	    <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/ >
	</bean>
    
        <!-- 相当于mybatis中的SqlSession -->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <!-- 只能使用构造器注入,因为他没有构造方法 -->
	    <constructor-arg index="0" ref="sqlSessionFactory" />
        </bean>
    
        <!-- 将UserMapperImpl注入到容器中 -->
	<bean id="userMapperImpl" class="com.qilu.mapper.UserMapperImpl" />
</beans>

6、编写测试类

public class MyTest {
    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}

Spring整合Mybatis

标签:ins   实现   setname   unit   names   bean   

原文:https://www.cnblogs.com/abc991314/p/13416572.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号