Mybatis:
通过xml或注解的方式将要执行的各种操作配置起来,并通过java对象和statement中的SQL进行映射生成SQL语句,最后由mybatis框架执行SQL将结果映射成java对象并返回。
快速入门-------增删改查
创建一个user类
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
public User() {
}
public User(String username, String sex, Date birthday, String address) {
this.username = username;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date 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 + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", address='" + address + '\'' +
'}';
}
}
创建mybatis-config.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>
<!--配置mybatis环境变量-->
<environments default="development">
<environment id="development">
<!--配置jdbc事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--配置dataSource-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/*****"/>
<property name="username" value="root"/>
<property name="password" value="*****"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
</configuration>
uerMapper.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="user">
<!--
[id]:statement的id,要求在命名空间内唯一
[parameterType]:入参的java类型
[resultType]:查询出的单条结果集对应的java类型
[#{}]: 表示一个占位符?
[#{id}]:表示该占位符待接收参数的名称为id。注意:如果参数为简单类型时,#{}里面的参数名称可以是任意定义
-->
<!--根据id查询-->
<select id="findUserById" parameterType="int" resultType="com.hetl.day01.pojo.User">
SELECT * from user where id = #{id}
</select>
<!--
[${}]:表示拼接SQL字符串
[${value}]:表示要拼接的是简单类型参数。
注意:
1、如果参数为简单类型时,${}里面的参数名称必须为value
2、${}会引起SQL注入,一般情况下不推荐使用。但是有些场景必须使用${},比如order by ${colname}
-->
<select id="findUserByName" parameterType="String" resultType="com.hetl.day01.pojo.User">
SELECT * from user where username like '%${value}%'
</select>
<insert id="insertUser" parameterType="com.hetl.day01.pojo.User">
insert into user (username,sex,birthday,address)
value (#{username},#{sex},#{birthday},#{address})
</insert>
<delete id="delectUser" parameterType="int">
delete from user where id = #{id}
</delete>
<update id="updateUser" parameterType="com.hetl.day01.pojo.User">
update user set address = #{address},sex=#{sex}
where id = #{id}
</update>
<!--
[selectKey标签]:通过select查询来生成主键
[keyProperty]:指定存放生成主键的属性
[resultType]:生成主键所对应的Java类型
[order]:指定该查询主键SQL语句的执行顺序,相对于insert语句
[last_insert_id]:MySQL的函数,要配合insert语句一起使用 -->
<insert id="insertUser02" parameterType="com.hetl.day01.pojo.User">
<selectKey keyProperty="id" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,sex,birthday,address)
value (#{username},#{sex},#{birthday},#{address})
</insert>
</mapper>
测试
package testDemo;
import com.hetl.day01.pojo.User;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
public class Test01 {
SqlSession sqlSession;
@Before
public void before() throws IOException {
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
}
@After
public void after() {
sqlSession.commit();
sqlSession.close();
}
@Test
public void test01() throws IOException {
User user=sqlSession.selectOne("findUserById",10);
System.out.println(user);
}
@Test
public void test02() throws IOException {
User user = new User("htl","1",new Date(),"河南");
sqlSession.insert("insertUser",user);
}
@Test
public void test03() throws IOException {
sqlSession.delete("delectUser",25);
}
@Test
public void test04() throws IOException {
User user = new User();
user.setId(22);
user.setAddress("河南");
user.setSex("3");
sqlSession.insert("updateUser",user);
}
@Test
public void test05() throws IOException {
User user = new User("hetl02","2",new Date(),"河南");
sqlSession.insert("insertUser02",user);
System.out.println(user.getId());
}
}
pom.xml所需要的依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.hetl.mybatis_study</groupId>
<artifactId>mybatis_study</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.26.0-GA</version>
</dependency>
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>