- 配置sqlMapConfig核心配置文件和Mapper映射文件
sqlMapConfig.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">
<!--核心文件,主要配置mybatis的核心内容,配置数据库的环境和加载映射(映射文件中需要使用到的别名也在此定义)-->
<configuration>
<!--加载jdbc.properties文件-->
<properties resource="jdbc.properties"></properties>
<!--自定义别名-->
<typeAliases>
<!--<typeAlias type="com.itheima.domain.User" alias="user"></typeAlias>-->
<package name="com.itheima.domain"/>
</typeAliases>
<!--配置数据源的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<!--<mapper resource="com.itheima.mapper/AccountMapper.xml"></mapper>-->
<package name="com.itheima.mapper"/>
</mappers>
</configuration>
AccountMapper.xml:
定义sql语句:参数和返回值的类型需要使用到核心配置文件中定义的别名
<?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">
<!--此映射文件主要配置sql语句-->
<mapper namespace="com.itheima.mapper.AccountMapper">
<select id="findAll" resultType="account">
select * from account
</select>
<select id="save" parameterType="account">
insert into account values(${id},${name},${money})
</select>
</mapper>
- 配置spring和spring-mvc配置文件
- 在applicationContext.xml中配置spring的组件扫描,扫描service和mapper,controller在spring-mvc中进行扫描,因此排除掉
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--组件扫描,扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!--排除controller,spring-mvc去扫描controller-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
- 配置spring-mvc.xml
扫描controller
配置注解驱动
配置视图解析器
开放静态资源访问权限
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--引入一下mvc的命名空间xmlns:mvc="http://www.springframework.org/schema/mvc"
和http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd-->
<!--组件扫描,扫描Controller-->
<context:component-scan base-package="com.itheima.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器,确定一下前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源权限开放-->
<mvc:default-servlet-handler/>
</beans>
- 配置web.xml