1.新建一个Maven项目
2. 在pom.xml中进行配置,在pom.xml中配置的时候,需要网速好,当网速不是很好的时候,是加载不出Jar包的。
代码如下所示.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"View Code
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.whl</groupId>
<artifactId>MyBatis01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatis01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
<build>
<finalName>MyBatis01</finalName>
</build>
</project>
<dependencies>的文件配置参考http://mvnrepository.com/
如果不知如何添加,可在此链接中输入相应jar包的名称,然后选择正确的版本,就会有示例。
3.手动导入MySQL的jar包,因为网速不好,下载不了,故采用手动导入,然后右键该jar包,build path。
4.在resources下,新建mybatis-config.xml。在此文件下需要完成数据库的连接和一组映射。代码如下
<?xml version="1.0" encoding="UTF-8"?>View Code
<!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/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/whl/mybatis/userMapper.xml" />
</mappers>
</configuration>
mappers中的映射要与下一步操作对应。
5.在Java文件夹下建立相应的包,并在此写相应的bean类,如下
package com.whl.mybatis;
public class User {
private int id;
private String username;
private String userpass;
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 getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", userpass=" + userpass + "]";
}
public User(int id, String username, String userpass) {
super();
this.id = id;
this.username = username;
this.userpass = userpass;
}
public User() {
super();
}
}
View Code
6.在Java文件下新建相关的包,新建一个userMapper.xml文件。在此文件中要完成相应的增删改查的编写,
代码如下:
<?xml version="1.0" encoding="UTF-8" ?>View Code
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper">
<!-- id是唯一标志符 -->
<!-- parameterType参数类型,指明插入时使用的参数类型 -->
<!-- useGeneratedKeys="true", 表示数据库的自动增长策略 -->
<insert id="save" parameterType="com.whl.mybatis.User"
useGeneratedKeys="true">
insert into user(username, password) values(#{username}, #{password})
</insert>
<delete id="deleteById" parameterType="int">
delete from user where uid = #{value}
</delete>
<delete id="deleteByIds">
delete from user where uid in
<foreach collection="list" item="uid" open="(" close=")" separator=",">
#{uid}
</foreach>
</delete>
<select id="likeByName" resultType="com.whl.mybatis.User" parameterType="string">
select * from user where username like '%${value}%'
</select>
<select id="findAll" resultType="com.whl.mybatis.User">
select uid, username, password from user
</select>
<select id="findById" parameterType="int" resultType="com.whl.mybatis.User">
select * from user where uid = #{value}
</select>
<update id="updateById" parameterType="com.whl.mybatis.User">
update user set username=#{username}, password=#{password} where uid = #{uid}
</update>
<!--
update user set username = case uid
when
#{u.uid} then #{u.username}
end
where uid in (...)
-->
<update id="updateUserBatch" parameterType="list">
update user set username = CASE uid
<foreach collection="list" item = "u" separator=" ">
when #{u.uid} then #{u.username}
</foreach>
end
where uid in
<foreach collection="list" item="user" open="(" separator="," close=")">
#{user.uid}
</foreach>
</update>
</mapper>
7.测试:代码如下
package com.whl.mybatis.test;View Code
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.whl.mybatis.User;
public class TestMyBatis {
private SqlSession s = null;
private SqlSessionFactory ssf = null;
@Before
public void init(){
String resourcesStr = "mybatis-config.xml";
try {
InputStream is = Resources.getResourceAsStream(resourcesStr);
ssf = new SqlSessionFactoryBuilder().build(is);
s = ssf.openSession();
}catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testDeleteByIds(){
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 36, 39);
// 以批量的处理方式打开数据库连接
ssf.openSession(ExecutorType.BATCH, false);
int result = s.delete("myBatis.deleteByIds", list);
s.commit();
System.out.println("result = " + result);
}
@Test
public void testDeleteById(){
int result = s.delete("myBatis.deleteByIds.deleteById", 38);
s.commit();
System.out.println("result = " + result);
}
@Test
public void testSelectByLike(){
String name = "user";
List<User> list = s.selectList("myBatis.deleteByIds.likeByName", name);
for (User u : list) {
System.out.println(u);
}
}
@Test
public void testUpdateBatch(){
List<User> users = new ArrayList<>();
User u = null;
for(int i = 0; i < 3; i++){
u = new User(20+i, "name" + i, "pass" + i);
System.out.println(u);
users.add(u);
}
int result = s.update("myBatis.deleteByIds.updateUserBatch", users);
s.commit();
System.out.println("result = " + result);
}
@Test
public void testUpdateUserById(){
User user = new User(38, "user38", "pass38");
int result = s.update("myBatis.deleteByIds.updateById", user);
System.out.println("result = " + result);
}
@Test
public void testFindById(){
User u = s.selectOne("myBatis.deleteByIds.findById", 32);
System.out.println(u);
}
@Test
public void testFindAll(){
List<User> list = s.selectList("myBatis.deleteByIds.findAll");
for (User u : list) {
System.out.println(u);
}
}
@Test
public void testAdd(){
try {
User user = new User();
user.setUsername("abc");
user.setPassword("123");
s.insert("myBatis.deleteByIds.save", user);
s.commit();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void destrory(){
if(s != null){
s.close();
s = null;
}
}
}