mybatis与spring的整合

2017-07-24  本文已影响0人  ccq_inori
首先在项目上导入所需的jar:(后面有视频资源,源码和jar的下载)
mybatis_spring.png
然后是创建好bean,我这里是用Student为例
public class Student 
{
    //对应上数据库的信息
    private String id;
    private String name;
    private String sex;
    private String age;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex
                + ", age=" + age + "]";
    }
}
然后是配置student.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="Student.Message.IStudent.IStudent">
    <resultMap type="Student.Message.bean.Student" id="Student">
   <!-- 拥有主键的话就用ID标签 -->
    <id column="ID" jdbcType="INTEGER" property="id"/> 
     <result column="NAME" jdbcType="VARCHAR" property="name"/>
     <result column="SEX" jdbcType="VARCHAR" property="sex"/>
     <result column="AGE" jdbcType="INTEGER" property="age"/>
    </resultMap>
    <select id="showStudent" parameterType="Student.Message.bean.Student" resultMap="Student">
        select * from student
    </select>
  </mapper>
mybatis_spring1.png
接下来是student.xml各种sql的实现接口
public interface IStudent 
{
     //显示全部学生信息
    public List<Student> showStudent();
}
配置总的mybatis文件
<configuration>
<!-- 配置log4j文件 -->
    <settings>  
        <setting name="logImpl" value="LOG4J"/>  
    </settings> 
  <mappers>
    <!-- Student.xml的全路径 -->
    <mapper resource="Student/Message/config/Student.xml"/>
  </mappers>
</configuration>
配置spring文件

这里引用了外部的文件来配置连接数据库
使用context的命名空间,注意:开头的网址,是用来引入各种命名空间

//这是db.properties文件
user=root
password=root
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test
<?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"
    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-3.0.xsd
    ">
    <context:property-placeholder location="classpath:db.properties" />
    
    <!-- 
        获取数据源
     -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverclass}"></property>
        <property name="jdbcUrl" value="${jdbcurl}"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池-->
        <property name="dataSource" ref="dataSource"></property>
        <!-- configLocation属性连接mybatis的核心配置文件 -->
        <property name="configLocation" value="Configuration.xml"></property>
    </bean>
    
    <bean id="showstudent" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象
            value:接口的全类名    
        -->
        <property name="mapperInterface" value="Student.Message.IStudent.IStudent"></property>
        <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    
</beans>
最后是测试类
public class Main 
{

    public static void main(String[] args) 
    {
        //用来接收从数据
        List<Student> studentlist=new ArrayList<Student>();
        //创建IOC容器
        //ClassPathXmlApplicationContext是ApplicationContext的实现类,从类路径来加载配置文件
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从ioc容器中获取bean实例
        //利用id来定位IOC容器中的bean
        IStudent istudent=(IStudent) ctx.getBean("showstudent");
        studentlist=istudent.showStudent();
        for(Student s:studentlist)
        {
            System.out.println(s.toString());
        }
    }
}
结果成功实现
搜狗截图17年07月20日1639_5.png

源码下载(http://pan.baidu.com/s/1bKHLls) 密码:jlv1

spring学习
mybatis学习

(http://pan.baidu.com/s/1i5n9ElR) 密码为:u07n

上一篇 下一篇

猜你喜欢

热点阅读