mybatis整合spring

一、建立Student的POJO包裝類:mysql

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String studentno;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private String point;
    private String phone;
    private String email;
    @Override
    public String toString() {
        return "Student{" +
                "studentno='" + studentno + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday='" + birthday + '\'' +
                ", classno='" + classno + '\'' +
                ", point='" + point + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public String getStudentno() {
        return studentno;
    }
    public void setStudentno(String studentno) {
        this.studentno = studentno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getClassno() {
        return classno;
    }
    public void setClassno(String classno) {
        this.classno = classno;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

二、dao層spring

(1)接口:sql

public class  StudentDaoImp extends SqlSessionDaoSupport implements StudentDao {

}

(2)實現類:數據庫

public class  StudentDaoImp extends SqlSessionDaoSupport implements StudentDao {

}

三、mapperapache

(1)接口mybatis

public interface StudentMapper {
    Student findStudentById(Integer id);
}

(2)配置:app

<?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="pers.zhb.mapper.StudentMapper">
    <!-- 經過ID查詢一個用戶 -->
    <select id="findStudentById" parameterType="Integer" resultType="Student">
        select *
        from student
        where studentno = #{v}
    </select>
</mapper>

四、核心配置文件ide

(1)spring: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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 數據庫鏈接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" /><!--最大鏈接數-->
        <property name="maxIdle" value="5" /><!--最大空閒-->
    </bean>

    <!-- Mybatis的工廠 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/><!-- 核心配置文件的位置 -->
    </bean>

    <!-- Dao原始Dao -->
    <bean id="StudentDao" class="pers.zhb.dao.StudentDaoImp">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>

    <!-- Mapper動態代理開發 -->
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <property name="mapperInterface" value="pers.zhb.mapper.StudentMapper"/>
    </bean>

    <!-- Mapper動態代理開發   掃描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="pers.zhb.mapper"/>
    </bean>


</beans>

(2)mybatis:sqlMapConfig.xmlthis

<?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>
        <!-- 2. 指定掃描包,會把包內全部的類都設置別名,別名的名稱就是類名,大小寫不敏感 -->
        <package name="pers.zhb.pojo" />
    </typeAliases>
    <mappers>
        <package name="pers.zhb.mapper"/>
    </mappers>

</configuration>

(3)數據庫參數:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu_mangement
jdbc.username=root
jdbc.password=root

五、測試:

public void testMapper() throws Exception {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentMapper mapper = ac.getBean(StudentMapper.class);
        Student student = mapper.findStudentById(201811);
        System.out.println(student);
    }

 

每一個人都會有一段異常艱難的時光 。 生活的壓力 , 工做的失意 , 學業的壓力。 愛的惶惶不可終日。 挺過來的 ,人生就會豁然開朗。 挺不過來的 ,時間也會教你 ,怎麼與它們握手言和 ,因此沒必要懼怕的。 ——楊絳
相關文章
相關標籤/搜索