Mybatis快速入门(mysql)---Maven环境
1、添加Mybatis jar包
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
2、本地数据库建表
3、添加Mybatis的配置文件,以及db.properties文件
db.properties文件
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3307/mybaits?serverTimezone=UTC
db.username=root
db.password=123456
Mybaits.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>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments>
</configuration>
4、 定义实体类
package nh.ui.automation.tools.Mybaits;
/**
* 项目 :UI自动化测试 Mybaits 类描述:
*
* @author Eric
* @date 2017年3月4日 nh.ui.automation.tools.Mybaits
*/
public class User {
private int id;
private String name;
private int age;
/**
* @param id
* @param name
* @param age
*/
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
/**
*
*/
public User() {
super();
}
/**
* @param name
* @param age
*/
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
5、定义操作user表的sql映射文件userMapper.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="nh.ui.automation.tools.Mybaits.User.userMapper"> <select id="getUser" parameterType="int" resultType="nh.ui.automation.tools.Mybaits.User"> select * from user where id=#{id} </select> </mapper>
6、在Mybaits.xml文件中注册userMapper.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> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="Mappings/userMapper.xml"/> </mappers> </configuration>
7、测试代码
public class App { public static void main( String[] args ) { /** * 1,加载mybaits的配置文件 * 2,构建sqlsession工厂 * 3,创建能执行sql的会话 * 4,映射sql的标识字符串 * 5,执行sql * 6,打印结果 */ String myBaitsConifg = "Mybaits.xml"; InputStream loadConfig = App.class.getClassLoader().getResourceAsStream(myBaitsConifg); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(loadConfig); SqlSession sqlSession = sessionFactory.openSession(true); //这里就是mybaits文件中的namespace+方法 String statement = "nh.ui.automation.tools.Mybaits.User.userMapper.getUser"; //执行sql并传递值 User user1 = sqlSession.selectOne(statement, 1); User user2 = sqlSession.selectOne(statement, 2); System.out.println(user1); System.out.println(user2); }
}