Mybatis简单入门
Posted SDingBa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis简单入门相关的知识,希望对你有一定的参考价值。
转载:
首先是环境的配置,我使用maven创建项目,pom.xml文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bird</groupId>
<artifactId>concursey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>concursey</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
</project>
还得创建一个数据库和对应的一张表做示范,这里就不写了,直接上对应的JavaBean
package com.bird.mybatis.bean;
public class Users
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 "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
然后要给对应的JavaBean创建对应的mapper.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.bird.mybatis.bean.userMapper">
<!--根据ID获取对应的值 -->
<select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">
select * from users where id = #id
</select>
</mapper>
当然还有mybatis的全局文件,conf.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>
<!-- development:开发模式 work:工作模式 -->
<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="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
</mappers>
</configuration>
最后是测试代码
package com.bird.mybatis.bean;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test
public static void main(String[] args) throws Exception
String resource = "conf.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "com.bird.mybatis.bean.userMapper.getUser";
Users user = session.selectOne(statement, 1);
System.out.println(user);
1,xml文件的使用;;
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.bird.mybatis.bean.userMapper">
<!--CRUD -->
<insert id="insertUser" parameterType="com.bird.mybatis.bean.Users">
insert into users(name, age) values(#name, #age);
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id = #id;
</delete>
<update id="updateUser" parameterType="com.bird.mybatis.bean.Users">
update users set name = #name, age = #age where id = #id;
</update>
<select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">
select * from users where id = #id;
</select>
<select id="getAllUser" resultType="com.bird.mybatis.bean.Users">
select * from users;
</select>
</mapper>
package com.bird.mybatis.bean;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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 MyTest
private SqlSessionFactory factory;
@Before
public void init()
String resource = "conf.xml";
Reader reader = null;
try
reader = Resources.getResourceAsReader(resource);
catch (IOException e)
e.printStackTrace();
factory = new SqlSessionFactoryBuilder().build(reader);
@Test
public void testAdd()
//默认是手动的
SqlSession session = factory.openSession();
String statement = "com.bird.mybatis.bean.userMapper.insertUser";
int rows = session.insert(statement, new Users(-1, "哈哈", 29));
//提交
session.commit();
System.out.println(rows);
session.close();
@Test
public void testUpdate()
SqlSession session = factory.openSession();
String statement = "com.bird.mybatis.bean.userMapper.updateUser";
session.update(statement, new Users(4, "呵呵", 29));
session.commit();
session.close();
@Test
public void testDelete()
SqlSession session = factory.openSession();
String statement = "com.bird.mybatis.bean.userMapper.deleteUser";
session.delete(statement, 1);
session.commit();
session.close();
@Test
public void testGetAllUser()
SqlSession session = factory.openSession();
String statement = "com.bird.mybatis.bean.userMapper.getAllUser";
List<Users> list = session.selectList(statement);
System.out.println(list.size());
session.close();
2 注解的使用:
上使用XML来操作Mybatis实现CRUD,但是大量的XML配置文件的编写是非常烦人的。因此
Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。
首先是创建一个接口
package com.bird.mybatis.bean;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface UserMapper
@Insert("insert into users(name, age) values(#name, #age)")
public int add(Users user);
@Delete("delete from users where id = #id")
public int deleteById(int id);
@Update("update users set name = #name, age = #age where id = #id")
public int update(Users user);
@Select("select * from users where id = #id")
public Users getUserById(int id);
@Select("select * from users")
public List<Users> getAllUsers();
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
<mapper class="com.bird.mybatis.bean.UserMapper"/>
</mappers>
@Test
public void testAdd2()
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.add(new Users(-1,"娃娃",99));
openSession.commit();
openSession.close();
h
z转载来着: http://blog.csdn.net/a352193394/article/details/39940259
以上是关于Mybatis简单入门的主要内容,如果未能解决你的问题,请参考以下文章