MyBatis笔记——初次环境配置

简单介绍

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)

MyBatis笔记——初次环境配置
MyBatis笔记——初次环境配置


1.导包

核心包
MyBatis笔记——初次环境配置

依赖包
MyBatis笔记——初次环境配置

当然还要有数据库驱动包


2.写xml配置

项目结构如下
MyBatis笔记——初次环境配置

2.1添加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/db_pwc" />
<property name="username" value="pwc" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/pwc/pojo/User.xml" />
</mappers>
</configuration>

2.2添加POJO类User.java

package com.pwc.pojo;
public class User {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
}
}

2.3添加POJO类User的映射文件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="com.pwc.dao.mapper.UserMapper">
<select id="findById" parameterType="int" resultType="com.pwc.pojo.User">
SELECT * FROM User WHERE id = #{id}
</select> <insert id="insert" parameterType="com.pwc.pojo.User">
INSERT INTO User(name,age) VALUES(#{name},#{age})
</insert> <delete id="deleteById" parameterType="int">
DELETE * FROM User WHERE id=#{id} </delete> <select id="findByAge" parameterType="int" resultType="com.pwc.pojo.User">
SELECT * FROM User WHERE age=#{age}
</select> </mapper>

3.测试

package com.pwc.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.pwc.pojo.User; public class Test { public static void main(String[] args) {
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(Test.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml")); SqlSession session = factory.openSession();
User user=session.selectOne("findById", 1);
System.out.println(user.toString()); } }

测试结果如下:
User [id=1, name=pwc, age=18]

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇:Sql server2005 优化查询速度50个方法小结


下一篇:(Android第一行代码实验一)活动的最佳实践