mybatis 学习三 关键文件解析

Posted 刘大飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 学习三 关键文件解析相关的知识,希望对你有一定的参考价值。

1:  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>    
         <typeAlias type="com.deppon.test04.entity.StudentEntity" alias="StudentEntity"/>   
    </typeAliases>   
    <mappers>  
        <mapper resource="StudentMapper.xml" />    <!--  可以定义多个mapper>
    </mappers>  
</configuration>  

2: StudentMapper.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.deppon.test04.dao.StudentMapper">  
    <resultMap type="StudentEntity" id="studentResultMap">  
        <id property="studentID" column="STUDENT_ID"/>  
        <result property="studentName" column="STUDENT_NAME"/>  
        <result property="studentSex" column="STUDENT_SEX"/>  
        <result property="studentBirthday" column="STUDENT_BIRTHDAY"/>  
    </resultMap>  
    <!-- 查询学生,根据id -->  
    <select id="getStudent" parameterType="String" resultType="com.deppon.test04.entity.StudentEntity" resultMap="studentResultMap">  
        <![CDATA[ 
            SELECT * from STUDENT_TBL ST 
                WHERE ST.STUDENT_ID = #{studentID}  
        ]]>   
    </select>  
    <!-- 查询学生列表 -->  
    <select id="getStudentAll"  resultType="com.deppon.test04.entity.StudentEntity" resultMap="studentResultMap">  
        <![CDATA[ 
            SELECT * from STUDENT_TBL 
        ]]>   
    </select>   
</mapper>  

3:  mysql.properties

jdbc.driverClassName=com.mysql.jdbc.Driver  
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=root&useUnicode=true&characterEncoding=UTF-8

4: dispatcherServlet

 <mvc:annotation-driven />  
    <context:component-scan base-package="liming.maven.example" /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
    <!-- 导入属性配置文件 -->  
    <context:property-placeholder location="classpath:mysql.properties" />  
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
            <property name="driverClassName" value="${jdbc.driverClassName}" />  
            <property name="url" value="${jdbc.url}" />  
    </bean>  
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    <property name="dataSource" ref="dataSource" />  
</bean>  
  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="configLocation" value="classpath:mybatis-config.xml" />  
    <property name="dataSource" ref="dataSource" />  
</bean>  
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="com.deppon.test04.dao.StudentMapper" />  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
</bean>  

 

以上是关于mybatis 学习三 关键文件解析的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis框架学习

Mybatis框架学习

mybatis学习笔记三(关联关系)

mybatis学习(39):动态sql片段

mybatis学习——config全局配置文件解析

解析Mybatis入门第三天