整合Mybatis
1.导入象关jar包(junit,mybatis,mysql,spring象关的,aop织入,mybatis-spring[new 知识点 mybatis 和谁整合就-谁])
2.编写配置
3.测试
2.mybatis
2.1编写实体类
2.2编写核心配置
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核心配置文件 -->
<configuration> <!--导入外部配置文件-->
<properties resource="db.properties"/>
<!--可以给实体类起别名-->
<typeAliases>
<package name="com.gs.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?userUnicode=true&characterEncoding=utf-8&&zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="root"/>
<property name="password" value="*******"/>
</dataSource>
</environment>
</environments>
<!-- 绑定 mapper -->
<mappers>
<!-- <mapper resource="com/com.com.gs/dao/UserMapper.xml"/>-->
<!--<mapper class="com.com.com.gs.dao.UserMapper"/>-->
<package name="com.gs.dao"/>
</mappers>
</configuration>
第二种方式
<!--properties 元素中直接配置 property 属性-->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?userUnicode=true&characterEncoding=utf-8&&zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="root"/>
<property name="password" value="*******"/>
</properties>
······
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
说明:上述代码中,driver、url、username、password 将会由 properties 元素中设置的值来替换。
需要配置maven静态资源过滤
<build>
<resources>
<resource>
<directory>xml输出路径</directory>
<includes>
<include>**/.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.3编写Mapper.xml
2.4测试成功