一、项目的结构目录
此处注意的是资源文件夹在conf的下面
二、mapper的xml文件配置
修改的部分
- 壹:改为Dao接口的全限定名称
- 贰:改为Dao接口中对应的方法的名称
- 叁:改为实体类对应的全限定名称,即查询操作后返回的类型(要求所有属性都要是有set方法,应为是要靠set注入传递值的)
<?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.bjpowernode.dao.StudentDao">
<select id="selectStudents--------贰--------" resultType="com.bjpowernode.damain.Student--------叁---------">
select id,name,age from student;
</select>
<insert id="insertStudent">
insert into student(name,age) values(#{name},#{age})
</insert>
</mapper>
三、spring的配置文件----applicationContext.xml
修改的位置
- 壹:连接数据库所需要的配置文件的地址
- 贰:加载mybatis中的配置文件
- 叁:加载所有的一个包下的mapper.xml文件
- 肆:加载一个包下的所有的带有@Service文件
<?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:util="http://www.springframework.org/schema/util"
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/util
https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
用于声明配置文件的位置,一使得下面的属性可以连接到有关于数据库的配置信息
-->
<context:property-placeholder location="--------壹-----classpath:conf/jdbc.properties"/>
<!--<util:property-path path="classpath:jdbc.properties"/>-->
<!--声明数据源Datasource,作用是连接数据库的-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--使用set注入给DruidDataSource赋值-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--表示最大的连接数量-->
<property name="maxActive" value="${jdbc.maxHuman}"/>
</bean>
<!--声明的是mybatis中提供的的sqlSessionFactory类,这个类中创建sqlSession的-->
<bean id="SqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--set注入,将阿里数据库的连接池赋值给dataSource-->
<property name="dataSource" ref="myDataSource"/>
<!--
mybatis中主配置文件的位置,用于读取主配置文件
-->
<property name="configLocation" value="--------贰-----classpath:conf/mybatis.xml"/>
</bean>
<bean