MyBatis创建流程
来源:互联网 发布:软件导刊 质量 编辑:程序博客网 时间:2024/05/21 08:52
1、数据库代码如下:
create database chapter3;
use chapter3;
create table t_role (
id int(12) auto_increment,
role_name varchar(60) not null,
note varchar(256) null,
primary key(id)
);
insert into t_role(role_name, note) values('role_name_1', 'note_1');
2、用到的包如下:
3、log4j.properties
log4j.rootLogger=DEBUG , stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
4、Role.java
5、RoleMapper.java
public interface RoleMapper {
public int insertRole(Role role);
public int deleteRole(Long id);
public int updateRole(Role role);
public Role getRole(Long id);
public List<Role> findRoles(String roleName);
}
6、RoleMapper.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.learn.ssm.chapter3.mapper.RoleMapper">
<insert id="insertRole" parameterType="role">
insert into t_role(role_name, note) values(#{roleName}, #{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from t_role where id= #{id}
</delete>
<update id="updateRole" parameterType="role">
update t_role set role_name = #{roleName}, note = #{note} where id= #{id}
</update>
<select id="getRole" parameterType="long" resultType="role">
select id,
role_name as roleName, note from t_role where id = #{id}
</select>
<select id="findRoles" parameterType="string" resultType="role">
select id, role_name as roleName, note from t_role
where role_name like concat('%', #{roleName}, '%')
</select>
</mapper>
7、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>
<typeAliases><!-- 别名 -->
<typeAlias alias="role" type="com.learn.ssm.chapter3.pojo.Role"/>
</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://localhost:3300/chapter3"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="com/learn/ssm/chapter3/mapper/RoleMapper.xml"/>
<mapper class="com.learn.ssm.chapter3.mapper.RoleMapper2"/>
</mappers>
</configuration>
8、SqlSessionFactory.java
public class SqlSessionFactoryUtils {
private final static Class<SqlSessionFactoryUtils> LOCK = SqlSessionFactoryUtils.class;
private static SqlSessionFactory sqlSessionFactory = null;
private SqlSessionFactoryUtils() {
}
public static SqlSessionFactory getSqlSessionFactory() {
synchronized (LOCK) {
if (sqlSessionFactory != null) {
return sqlSessionFactory;
}
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}
//代码生成SqlSessionFactory
public static SqlSessionFactory getSqlSessionFactory2() {
synchronized (LOCK) {
//数据库连接池信息
PooledDataSource dataSource = new PooledDataSource();
dataSource.setDriver("com.mysql.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("123456");
dataSource.setUrl("jdbc:mysql://localhost:3306/chapter3");
dataSource.setDefaultAutoCommit(false);
//采用MyBatis的JDBC事务方式
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
//创建Configuration对象
Configuration configuration = new Configuration(environment);
//注册一个MyBatis上下文别名
configuration.getTypeAliasRegistry().registerAlias("role", Role.class);
//加入一个映射器
configuration.addMapper(RoleMapper.class);
configuration.addMapper(RoleMapper2.class);
//使用SqlSessionFactoryBuilder构建SqlSessionFactory
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(configuration);
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession() {
if (sqlSessionFactory == null) {
getSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
9、Chapter3Main.java
public class Chapter3Main {
public static void main(String[] args) {
testRoleMapper();
testRoleMapper2();
}
private static void testRoleMapper() {
Logger log = Logger.getLogger(Chapter3Main.class);
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1L);
log.info(role.getRoleName());
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
//注解SQL测试
private static void testRoleMapper2() {
Logger log = Logger.getLogger(Chapter3Main.class);
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
RoleMapper2 roleMapper2 = sqlSession.getMapper(RoleMapper2.class);
Role role = roleMapper2.getRole(1L);
log.info(role.getRoleName());
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
10、测试
- mybatis创建对象流程
- MyBatis创建流程
- Mybatis运作流程(手动方式创建sqlSessionFactory)
- mybatis与spring集成中SqlSessionFactory创建流程
- Mybatis流程
- [Mybatis] Mybatis运作流程分析
- IntelliJ IDEA下SSM框架整合、创建Maven项目流程(Spring + SpringMVC + MyBatis)
- Mybatis分页实现流程
- MyBatis流程和框架
- MyBatis执行流程
- myBatis的工作流程
- mybatis工作流程
- Mybatis的工作流程
- Mybatis操作主体流程
- MyBatis启动流程
- Mybatis启动流程详解
- mybatis代码流程-未完成
- mybatis框架 流程分析
- java面试题
- 计算机系统的初步学习(持续更新)--关于replace_byte()函数
- HDU 2594 Simpsons’ Hidden Talents
- 常见的HTTP状态码(HTTP Status Code)说明
- 7-85 红色警报(25 分)
- MyBatis创建流程
- 数组和结构
- SpringMVC中的/*和/的区别
- 闭包
- heartbeat+Haproxy多VIP负载均衡高可用
- java面试题上
- HDU1823 Luck and Love (二维线段树 + 单点更新 + 区间查询)
- 三个孩子年龄相乘是36,将三个孩子的年龄从大到小输出的智力编程题
- Chinese Zodiac---ACM青岛Regional