众所周知,Mybatis是一个JAVA轻量级(相对于Hibernate与)的数据库处理框架,支持xml映射文件、动态SQL。对于习惯了写复杂SQL语句的开发人员来说,是非常方便的,他摒除了jdbc的冗长写法,极大地减少了代码行数。不同于Hibernate繁琐的配置,Mybatis的集成是非常容易的。
1.新建新建实体类:
public Class User{
public String user_id;
public String user_name;
}
2.新建映射接口:com.company.mapInterface.UserMapper
public interface UserMapper {
public User findByUserId(String userId);
}
3.新建xml映射文件com/company/mappers/UserMapping.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">
<!--这块等于dao接口的实现 namespace必须和接口的类路径一样-->
<mapper namespace="com.company.mapInterface.UserMapper">
<!-- findByUserId必须和接口中的方法名一样 返回一个User就是刚才的别名-->
<select id="findByUserId" parameterType="HashMap" resultType="User">
select * from user_data where user_id=#{userId}
</select>
</mapper>
4.在com.company.config下新建batis配置文件mybatis.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>
<typeAliases>
<!—作用:该包下所有实体类自动使用别名 -->
<package name="com.company.bean"/>
</typeAliases>
<!--数据源配置 使用mysql数据库 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Mysql写法-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> -->
<!--Oracle写法-->
<property name="driver" value="oracle.jdbc.OracleDriver"></property>
<property name=" url"value="jdbc:oracle:thin:@XXX"></property>
<!--账号/密码-->
<property name="username" value="test" />
<property name="password" value="psw" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
其中dataSource 的配置也可以使用占位符,如下:
<?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>
<typeAliases>
<!—作用:该包下所有实体类自动使用别名 -->
<package name="com.company.bean"/>
</typeAliases>
<properties resource="org/mybatis/example/config.properties">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@XXX"/>
<property name="username" value="test"/>
<property name="password" value="psw"/>
</properties>
<!--数据源配置 使用mysql数据库 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Oracle写法-->
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<!--账号/密码-->
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
还可以使用配置文件,如下:
<?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>
<typeAliases>
<!—作用:该包下所有实体类自动使用别名 -->
<package name="com.company.bean"/>
</typeAliases>
<properties resource="jdbc.properties"></properties>
<!--数据源配置 使用mysql数据库 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Mysql写法-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> -->
<!--Oracle写法-->
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<!--账号/密码-->
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
然后同路径下新建jdbc.properties文件
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@XXX
username=test
password=psw
5.最后一步,测试:
(1)新建工具类:
public class BatisUtil {
public static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "com/company/config/mybatis.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sessionFactory;
(2)新建测试类:
public class MyBatisTest {
public static void main(String[] args) {
SqlSession sqlSession = BatisUtil.getSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findByUserId("1455");
System.out.println(user.user_name);
}
}
执行,正确打印出数据库中的user_name值,完成。