MyBatis main configuration file SqlMapConfig.xml description :( note the order) official blog: Portal
1, properties (read configuration files)
2, Settings (global configuration parameters)
. 3, typeAliases (type alias)
. 4, typeHandlers (processor type)
. 5, the objectFactory (object factory)
. 6, plugins (plug-in)
. 7, Environments (Environment collection property objects, the integration with the Spring framework Good Bye Say)
A) environment (environment Object sub-attribute)
B) the transactionManager (transaction management)
C) the dataSource (data source)
. 8, by mappers (configuration mapping position)
1, using properties read db.properties profile
db.properties profile
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_mybatis jdbc.username=root jdbc.password=123456
sqlMapConfig.xml properties used to read the configuration file
<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
<?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>
2, sqlMapConfig-typeAliases Alias Configuration
Alias arranged in two ways, one is to separate bean alias object configuration, and the other is disposed to direct package alias
<! - Alias Configuration -> < typeAliases > < typeAlias of the type = "com.Gary.bean.User" Alias = "the User" /> <! - alias by the name of his class to get a direct User (no difference case) -> < Package name = "com.Gary.bean" /> </ typeAliases >
Bean objects arranged to separate alias
Configuring com.Gary.bean.User type of typeAliases alias file under sqlMapConfig.xml
<!-- 别名配置 --> <typeAliases> <typeAlias type="com.Gary.bean.User" alias="user"/> </typeAliases>
Query a user in return UserMapper go right back to his 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>
<?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>
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); } }
给整个包配置别名(别名为类名,不区别大小写,推荐使用这种方式配置别名)
<?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>
<?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>
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); } }
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>