Mybatis框架|第一个Mybatis程序

文章说明:第一个Mybatis程序,打印出数据库中的一条记录


一、Mybatis框架环境搭建

使用eclipse创建一个Java项目,在src下创建三个包:

  • com.gql.Demo:用来写测试类。
  • com.gql.pojo:用来写JavaBean。
  • com.gql.sql:用来存储sql语句。

然后创建lib文件夹,导入Mybatis相关jar包,
在这里插入图片描述

二、创建数据库表user

创建数据库表,并添加记录。

CREATE TABLE `user` (
	`id` INT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
	`username` VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
	`sex` VARCHAR(1) NULL DEFAULT NULL COMMENT '性别',
	`address` VARCHAR(200) NULL DEFAULT NULL COMMENT '住址',
	`birthday` DATE NULL DEFAULT NULL COMMENT '生日',
	PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

在这里插入图片描述

三、编写JavaBean

package com.gql.pojo;

import java.io.Serializable;
import java.util.Date;
/**
 * 类说明:
 *		JavaBean
 * @guoqianliang1998.
 */
public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private String sex;
	private String address;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

四、sql语句配置XML

在src上右键new一个统计文件夹Source Folder命名为config,在config中创建User.xml,配置sql语句。

  • 值得注意的是,Mybatis中的sql语句中字段必须和数据库中一致,而不是和JavaBean中保持一致。如果对应不一致,可以使用mysql中的as。
<?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="sql1" parameterType="int" resultType="com.gql.pojo.User">
		SELECT id,username as name,sex,address,birthday FROM user WHERE id = #{id};
	</select>
</mapper>

五、全局配置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://localhost:3306/mybatis" />
				<property name="username" value="root" />
				<property name="password" value="Hudie" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="User.xml" />
	</mappers>
</configuration>

六、使用Mybatis框架进行测试

package com.gql.Demo;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

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.gql.pojo.User;
/**
 * 类说明:
 *		第一个Mybatis程序:打印出数据库中的一条记录
 * @guoqianliang1998.
 */
public class Demo {
	@Test
	public void testSelectOne() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user = session.selectOne("test.sql1", 1);
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

			System.out.println(user.getId()+","+user.getName()+","+user.getSex()+","+user.getAddress()+","+df.format(user.getBirthday()));
		} finally {
			session.close();
		}

	}
}

成功打印出数据:
在这里插入图片描述

发布了391 篇原创文章 · 获赞 1044 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/weixin_43691058/article/details/104204050