Mybatis学习02
title: '''Mybatis学习02'';' date: 2020-01-19 11:20:05 tags:
1、Mybatis核心配置文件
-
XML 配置文件中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。
-
官方推荐XML配置文件命名为:mybatis-config.xml
XML文件放置的路径为:main/resources
<?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> //默认的环境id为“development” <environments default="development"> //一个环境的配置 <environment id="development"> //事务管理器类型为JDBC,还有一个MANAGED,但是几乎不用了 <transactionManager type="JDBC"/> //数据源为一个池子 <dataSource type="POOLED"> //连接数据库的各种配置 <property name="driver" value="driver"/> <property name="url" value="{url"/> <property name="username" value="username"/> <property name="password" value="password"/> </dataSource> </environment> </environments> </configuration>
2、测试的准备
-
Jave Bean
实体类的创建和之前的创建方式一样。
package com.neversettle.domain; public class User { private int id; private String username; private String birthday; private String sex; private String address; public User() { } public User(int id, String username, String birthday, String sex, String address) { this.id = id; this.username = username; this.birthday = birthday; this.sex = sex; this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday='" + birthday + '\'' + ", sex=" + sex + ", address='" + address + '\'' + '}'; } }