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>