(正在补充)
1.从第一个程序开始,通过mybatis实现数据库表内容的增删改查
(源码zip包)
1.0准备工作
建数据库mybatis,在其中创建表sql语句如下:
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `orders` -- ---------------------------- DROP TABLE IF EXISTS `orders`; CREATE TABLE `orders` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `user_id` INT(11) NOT NULL COMMENT '下单用户id', `number` VARCHAR(32) NOT NULL COMMENT '订单号', `createtime` DATETIME NOT NULL COMMENT '创建订单时间', `note` VARCHAR(100) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`), KEY `FK_orders_1` (`user_id`), CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of orders -- ---------------------------- INSERT INTO `orders` VALUES ('3', '1', '1000010', '2015-02-04 13:22:35', NULL); INSERT INTO `orders` VALUES ('4', '1', '1000011', '2015-02-03 13:22:41', NULL); INSERT INTO `orders` VALUES ('5', '10', '1000012', '2015-02-12 16:13:23', NULL); -- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(32) NOT NULL COMMENT '用户名称', `birthday` DATE DEFAULT NULL COMMENT '生日', `sex` CHAR(1) DEFAULT NULL COMMENT '性别', `address` VARCHAR(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', '王五', NULL, '2', NULL); INSERT INTO `user` VALUES ('10', '张三', '2014-07-10', '1', '北京市'); INSERT INTO `user` VALUES ('16', '张小明', NULL, '1', '河南郑州'); INSERT INTO `user` VALUES ('22', '陈小明', NULL, '1', '河南郑州'); INSERT INTO `user` VALUES ('24', '张三丰', NULL, '1', '河南郑州'); INSERT INTO `user` VALUES ('25', '陈小明', NULL, '1', '河南郑州'); INSERT INTO `user` VALUES ('26', '王五', NULL, NULL, NULL);
1.1新建java项目(mytatis-a1)
创建lib文件夹,加入mybaties核心包、依赖包、数据库包(整合包点我下载),build to path
创建config资源文件,放置配置文件
config/创建sqlMapConfig.xml(核心配置文件)
config/创建log4j.properties(日志)
config/创建sqlmap/user.xml(写sql语句)
创建包com.mybatis.pojo/下根据数据库表创建User.java
com.mybatis.test/MybatiesTest.java
结构图如下:
填写代码:
sqlMapConfig.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://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> dataSource> environment> environments> <mappers> <mapper resource="sqlmap/user.xml"/> mappers> configuration>
log4j.properites
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
user.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="test"> <select id="queryUserById" parameterType="Integer" resultType="com.mybaties.pojo.User"> select * from user where id = #{v} select> mapper>
User.java
package com.mybaties.pojo; import java.io.Serializable; import java.util.Date; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } }
MybatisTest.java
package com.mybaties.test; 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.Before; import org.junit.Test; public class MybatiesTest { private SqlSessionFactory factory; @Before public void init() throws Exception{ InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); this.factory = builder.build(in); } @Test public void testQueryUserById(){ SqlSession session = factory.openSession(); Object user = session.selectOne("test.queryUserById", 37); System.out.println(user); } }
MybatiesTest.java
package com.mybaties.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; public class MybatiesTest1 { @Test public void testQueryUserById() throws IOException{ InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); Object user = session.selectOne("test.queryUserById", 37); System.out.println(user); } }
junit:MybatiesTest中testQueryUserById结果如图: