如何在Spring项目中使用Mybatis

warning:下面一切的内容必需基于你的spring能正常使用,不能使用的你还是先不要看。。。zzz

一、准备工作

  1. jar包下载
    mybatis-3.4.4.jar
    mybatis-spring-1.3.1.jar
    spring-jdbc-4.2.4.RELEASE.jar
    mysql-connector-Java-5.1.10.jar(由于我这里使用mysql为例)
    spring-tx-4.2.4.RELEASE.jar
  2. mysql建库建表初始化数据
create database studentDB;
use studentDB;
create table student(id char(11) primary key,name varchar(30),age tinyint,sex char(1));
insert into student values('12014052074','xyh',21,'男');
insert into student values('1201405207x','xiaojiejie',18,'女');

二、定义bean类和dao接口以及映射XML文件

1、bean(student.java)

@Component//该处注解该bean为一个spring托管的bean
public class Student {
    private String id;
    private String name;
    private byte age;
    private String sex;
    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 int getAge() {
        return age;
    }
    public void setAge(byte age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}

2、 dao(studentDao.java)

public interface StudentDao {
    public int updateStudent(Student s);\\更新记录接口方法
    public List<Student> getAllStudent();\\获取所有表行记录
}

3、XML(studentDao.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="com.xyh.dao.StudentDao">
    <update id="updateStudent" parameterType="com.xyh.beans.Student">
        update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}
    </update>
    <resultMap type="com.xyh.beans.Student" id="students">
        <id column="id" property="id" jdbcType="CHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="TINYINT" />
        <result column="sex" property="sex" jdbcType="CHAR"/>
    </resultMap>
    <select id="getAllStudent" resultMap="students">
        select * from student
    </select>
</mapper>

mapper标签的namespace属性为该映射文件对应Dao接口类。
update标签用于更新操作,id属性为对应方法名,paramerType属性为传入方法的参数类型,标签体为操作sql,#{x}为传入参数bean的x属性。
resultMap标签定义返回映射集,由于select操作中返回的结果需要存储为list集合,type属性为集合中元素类型,id标签对应数据库主键列,column属性为表中字段名,property为bean中属性名,jdbcType为表字段类型(注意:并不是与表中类型名称都一样)
select 标签用于查询操作,id属性对应方法名,resultMap属性为返回类型,因为是结果集合,使用前面定义的resultMap。

4、mybatis.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>
    <mappers>
        <mapper resource="com/xyh/dao/StudentDao.xml"/>
    </mappers>
</configuration>

引入定义的studentDao.xml文件

三、配置spring.xml配置文件

1、配置mysql数据源

<bean id="dataSource"                   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/newsDB?characterEncoding=UTF-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="12345"></property>
</bean>

2、配置会话工厂

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
            <property name="dataSource" ref="dataSource" />
</bean>

3、配置studentDao

<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="com.xyh.dao.StudentDao"></property>  
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
    </bean> 

好了,该配置该定义该准备的都完成了,让我们来看看结果吧

四、 test

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext atc = new ClassPathXmlApplicationContext("spring.xml");
        Student s = (Student) atc.getBean("student");
        s.setId("12014052074");
        s.setName("xyh");
        s.setAge((byte)22);
        s.setSex("男");

        StudentDao dao = (StudentDao) atc.getBean("studentDao");
        System.out.println(dao.updateStudent(s));
    }

}

输出结果为1(影响数据行数),这时候去数据库一看,噢,我又老了一岁了。。。zzz

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext atc = new ClassPathXmlApplicationContext("spring.xml");      
        StudentDao dao = (StudentDao) atc.getBean("studentDao");
        List<Student> students = dao.getAllStudent();
        System.out.println(students.size());
    }
}

输出结果为2,数据集的数据size。
好了,到这里你还整合不了的话~~~~~~~你还是从头再来吧。。。zzz

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值