MyBatis3??????__01HelloWorld

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis3??????__01HelloWorld相关的知识,希望对你有一定的参考价值。

???????????????   out   ???????????????   utf8   struct   ?????????   javabean   var   employee   

# MyBatis????????????ORM?????????????????????????????????????????????
??????????????????????????????
???????????????????????????MyBatis???????????????????????????
## 1.?????????????????????mybatis_learn??????????????????tbl_employee???
`CREATE DATABASE mybatis_learn;`
```
CREATE TABLE `tbl_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
```
## 2.????????????JavaBean(?????????lombok)???
```
package com.mybatis.learn.bean;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee {
private Integer id;
private String lastName;
private String gender;
private String email;
}
```
## 3.???????????????Mybatis???
### 3.1 ??????????????????jar???
```
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
```
### 3.2??????SqlSessionFactory??????/??????xml?????????
??????xml?????????
```
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
```
??????????????????????????????????????????????????????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>
<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://192.168.1.61:3306/mybatis_learn" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- ??????????????????sql???????????????EmployeeMapper.xml??????????????????????????????????????????mybatis-config.xml?????? -->
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
```

????????? ???????????????????????????xml???????????????
```
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
```

###3.3??? SqlSessionFactory ????????? SqlSession
```
SqlSession session = sqlSessionFactory.openSession();
try {
Employee employee = (Employee) session.selectOne("org.mybatis.example.BlogMapper.selectEmployee", 1);
System.out.println(employee);
} finally {
session.close();
}
```
???????????????????????????sql???????????????
```
<?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="org.mybatis.example.BlogMapper">
<select id="selectEmployee" resultType="com.mybatis.learn.bean.Employee">
select * from tbl_employee where id = #{id}
</select>
</mapper>
```
?????????????????????????????????(lastName ????????????????????????????????????)

以上是关于MyBatis3??????__01HelloWorld的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis学习总结_08_Mybatis3.x与Spring4.x整合

01_Mybatis基础

mybatis3.2.7应用_高级映射(一对一对多多对多)

黑马java代码01-03.docx

Mybatis3源码学习之二JDBC

Mybatis_总结_04_用_注解