系列博文:
JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二 传送门
JavaWeb_(Mybatis框架)Mapper动态代理开发_三 传送门
JavaWeb_(Mybatis框架)主配置文件介绍_四 传送门
JavaWeb_(Mybatis框架)输入和输出参数_五 传送门
JavaWeb_(Mybatis框架)关联查询_六传送门 传送门
JavaWeb_(Mybatis框架)动态sql_七传送门 传送门
MyBatis 主配置文件SqlMapConfig.xml说明:(注意顺序) 官方博客:传送门
1、properties(读取配置文件)
2、settings(全局配置参数)
3、typeAliases(类型别名)
4、typeHandlers(类型处理器)
5、objectFactory(对象工厂)
6、plugins(插件)
7、environments(环境集合属性对象,与Spring框架整合后Say Good Bye)
a)environment(环境子属性对象)
b)transactionManager(事务管理)
c)dataSource(数据源)
8、mappers(配置映射器位置)
1、使用properties读取db.properties配置文件
db.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_mybatis
jdbc.username=root
jdbc.password=123456
sqlMapConfig.xml中使用properties读取配置文件
<properties resource="db.properties"/> <dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_mybatis
jdbc.username=root
jdbc.password=123456
db.properties
<?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>
<!-- 读取配置文件 -->
<properties resource="db.properties"/> <environments default="development">
<environment id="development">
<!-- 使用JDBC的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池链接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers> </configuration>
sqlMapConfig.xml
2、sqlMapConfig-typeAliases别名配置
配置别名有两种方式,一种是单独给bean对象配置别名,另一种是直接给包配置别名
<!-- 别名配置 -->
<typeAliases>
<typeAlias type="com.Gary.bean.User" alias="user"/>
<!-- 别名直接按他的类名去取的User(不区别大小写) -->
<package name="com.Gary.bean"/>
</typeAliases>
单独给bean对象配置别名
在sqlMapConfig.xml文件下配置com.Gary.bean.User类型的typeAliases别名
<!-- 别名配置 -->
<typeAliases>
<typeAlias type="com.Gary.bean.User" alias="user"/>
</typeAliases>
UserMapper下的查询一个用户返回就可以直接返回他的user
<select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select>
<?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>
<!-- 读取配置文件 -->
<properties resource="db.properties"/> <!-- 别名配置 -->
<typeAliases>
<typeAlias type="com.Gary.bean.User" alias="user"/>
</typeAliases> <environments default="development">
<environment id="development">
<!-- 使用JDBC的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池链接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers> </configuration>
sqlMapConfig.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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import com.Gary.bean.User;
import com.Gary.mapper.UserMapper; public class MapperTest { @Test
public void Test1() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.selectUserById(1); System.out.println(user); } }
MapperTest.java
给整个包配置别名(别名为类名,不区别大小写,推荐使用这种方式配置别名)
<?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>
<!-- 读取配置文件 -->
<properties resource="db.properties"/> <!-- 别名配置 -->
<typeAliases>
<!-- 别名直接按他的类名去取的User(不区别大小写) -->
<package name="com.Gary.bean"/>
</typeAliases> <environments default="development">
<environment id="development">
<!-- 使用JDBC的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池链接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers> </configuration>
sqlMapConfig.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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> </mapper>
UserMapper.xml
package com.Gary.test; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import com.Gary.bean.User;
import com.Gary.mapper.UserMapper; public class MapperTest { @Test
public void Test1() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.selectUserById(1); System.out.println(user); } }
MapperTest.java
3、sqlMapConfig-mapper映射器位置 官方文档:传送门
<!-- 配置映射器的位置 推荐使用以包的形式配置 -->
<mappers>
<!-- 单个配置:使用相对于类路径的资源引用 -->
<mapper resource="mapper/UserMapper.xml"/>
<!-- 单个配置:使用完全限定资源定位符(URL) System Explorer位置 -->
<mapper url="file:\\\F:\SSH\pro\ssm_mybatis\src\mapper">
<!-- 单个配置:使用映射器接口实现类的完全限定类名 UserMapper接口必须和.xml文件在同,且在同一个文件之下,且名字一样-->
<mapper class="com.Gary.mapper.UserMapper"/> <!-- 将包内的映射器接口实现全部注册为映射器 接口必须与.xml文件相同,且在同一个文件之下,且名字一样-->
<package name="com.Gary.mapper"/> </mappers>
<?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>
<!-- 读取配置文件 -->
<properties resource="db.properties"/> <!-- 别名配置 -->
<typeAliases>
<!-- 别名直接按他的类名去取的User(不区别大小写) -->
<package name="com.Gary.bean"/>
</typeAliases> <environments default="development">
<environment id="development">
<!-- 使用JDBC的事务 -->
<transactionManager type="JDBC"/>
<!-- 使用连接池链接数据库 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments> <!-- 配置映射器的位置 推荐使用以包的形式配置 -->
<mappers>
<!-- 单个配置:使用相对于类路径的资源引用 -->
<mapper resource="mapper/UserMapper.xml"/>
<!-- 单个配置:使用完全限定资源定位符(URL) System Explorer位置 -->
<mapper url="file:\\\F:\SSH\pro\ssm_mybatis\src\mapper">
<!-- 单个配置:使用映射器接口实现类的完全限定类名 UserMapper接口必须和.xml文件在同,且在同一个文件之下,且名字一样-->
<mapper class="com.Gary.mapper.UserMapper"/> <!-- 将包内的映射器接口实现全部注册为映射器 接口必须与.xml文件相同,且在同一个文件之下,且名字一样-->
<package name="com.Gary.mapper"/> </mappers> </configuration>
sqlMapConfig.xml