Created
February 7, 2015 06:56
-
-
Save cheng470/7900bdfd0975aebad371 to your computer and use it in GitHub Desktop.
Mybatis 第一个例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<!-- 事务管理器 ==在 MyBatis 中有两种类型的事务管理器(也就是 type=”[JDBC|MANAGED]”) | |
JDBC – 这个配置就是直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务范围。 | |
Note: 如果你正在使用 Spring + MyBatis,则没有必要配置事务管理器,因为 Spring 模块会使用自带的管理器来覆盖前面的配置。 | |
--> | |
<transactionManager type="JDBC"/> | |
<!-- dataSource 元素使用基本的 JDBC 数据源接口来配置 JDBC 连接对象的资源。 --> | |
<dataSource type="POOLED"> | |
<property name="driver" value="com.mysql.jdbc.Driver"/> | |
<property name="url" value="jdbc:mysql://localhost:3306/test"/> | |
<property name="username" value="root"/> | |
<property name="password" value="123456"/> | |
</dataSource> | |
</environment> | |
</environments> | |
<mappers> | |
<mapper resource="userMapper.xml"/> | |
</mappers> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.bean; | |
public class User { | |
private Integer id; | |
private String name; | |
private String password; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
@Override | |
public String toString() { | |
return "User [id=" + id + ", name=" + name + ", password=" + password | |
+ "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- -------------------------------------------------------- | |
-- Host: 127.0.0.1 | |
-- Server version: 5.6.15-enterprise-commercial-advanced - MySQL Enterprise Server - Advanced Edition (Commercial) | |
-- Server OS: Win64 | |
-- HeidiSQL Version: 8.0.0.4396 | |
-- -------------------------------------------------------- | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET NAMES utf8 */; | |
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | |
-- Dumping structure for table test.user | |
DROP TABLE IF EXISTS `user`; | |
CREATE TABLE IF NOT EXISTS `user` ( | |
`id` int(10) NOT NULL AUTO_INCREMENT, | |
`name` varchar(50) NOT NULL, | |
`password` varchar(32) NOT NULL, | |
`email` varchar(32) NOT NULL, | |
`mobile` varchar(32) NOT NULL, | |
`status` tinyint(4) NOT NULL, | |
`createtime` datetime NOT NULL, | |
`changetime` datetime NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; | |
-- Dumping data for table test.user: ~3 rows (approximately) | |
DELETE FROM `user`; | |
/*!40000 ALTER TABLE `user` DISABLE KEYS */; | |
INSERT INTO `user` (`id`, `name`, `password`, `email`, `mobile`, `status`, `createtime`, `changetime`) VALUES | |
(1, 'admin', 'admin', '[email protected]', '12312341234', 1, '2015-02-07 13:29:52', '2015-02-07 13:29:53'), | |
(2, 'zhangsan', 'zhangsan', '[email protected]', '12312341234', 1, '2015-02-07 13:34:45', '2015-02-07 13:34:45'), | |
(3, 'lisi', 'lisi', '[email protected]', '12312341234', 1, '2015-02-07 13:45:58', '2015-02-07 13:45:59'); | |
/*!40000 ALTER TABLE `user` ENABLE KEYS */; | |
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; | |
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="User"> | |
<select id="listUser" resultType="com.test.bean.User"> | |
select id, name, password | |
from user | |
</select> | |
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.dao.mybatis; | |
import java.io.IOException; | |
import java.io.Reader; | |
import java.util.List; | |
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.test.bean.User; | |
public class UserTest { | |
/** | |
* 获得MyBatis SqlSessionFactory | |
* SqlSessionFactory负责创建SqlSession,一旦创建成功,就可以用SqlSession实例来执行映射语句 | |
* ,commit,rollback,close等方法。 | |
* | |
* @return | |
*/ | |
private static SqlSessionFactory getSessionFactory() { | |
SqlSessionFactory sessionFactory = null; | |
String resource = "configuration.xml"; | |
try { | |
Reader reader = Resources.getResourceAsReader(resource); | |
sessionFactory = new SqlSessionFactoryBuilder().build(reader); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return sessionFactory; | |
} | |
@Test | |
public void test() { | |
SqlSession sqlSession = getSessionFactory().openSession(); | |
List<User> users = sqlSession.selectList("User.listUser"); | |
for (User u: users) { | |
System.out.println(u); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment