Mybatis简单注解使用

1:pom.xml文件配置

<dependencies>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
2:jdbc.properties配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbc1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=010429

3:log4j.properties配置
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
4: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>
<!-- 通过properties标签加载外部文件-->
<properties resource="jdbc.properties"></properties>

<environments default="development">
<!--配置mysql的环境,注意,default中的属性,与下边environment中id的属性是一样的-->
<environment id="development">
<!--配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源,也叫作连接池-->
<dataSource type="POOLED">
<!--配置连接数据库的四个基本信息-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>

<!--加载映射关系-->
<mappers>
<!-- 指定接口所在得包-->
<package name="com.xiaoze.mapper"/>
</mappers>
</configuration>
5Users实现
package com.xiaoze.domian;

public class Users {
private int id;
private String name;
private String password;
private String email;
private String birthday;

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 String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Users{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
}
6:UsersMapper接口实现
package com.xiaoze.mapper;

import com.xiaoze.domian.Users;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UsersMapper {

@Insert("insert into users values(#{id},#{name},#{password},#{email},#{birthday})")
public void save(Users users);

@Update("update users set password = #{password} where id = #{id}")
public void update(Users users);

@Delete("delete from users where id = #{id}")
public void delete(int id);

@Select("select * from users where id = #{id}")
public Users findById(int id);

@Select("select * from users")
public List<Users> findAll();
}
7:测试类实现
package com.xiaoze.Test;

import com.xiaoze.domian.Users;
import com.xiaoze.mapper.UsersMapper;
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;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
* @author 小泽
* @create 2021-12-04 9:15
* 记得每天敲代码哦
*/
public class test {

private UsersMapper mapper ;

@Before
public void before() throws IOException {
//获取核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获取session工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获取session会话对象
SqlSession sqlSession = build.openSession(true);
mapper = sqlSession.getMapper(UsersMapper.class);
}


@Test
//注解添加
public void testSave(){
Users users = new Users();
users.setName("ooosss");
users.setPassword("huhaoze");
users.setEmail("ajcak@qq.com");
users.setBirthday("1952-10-3");
mapper.save(users);
}

@Test
//注解修改
public void testUpdate(){
Users users = new Users();
users.setId(9);
users.setPassword("HHZ");
mapper.update(users);
}

@Test
//注解删除
public void testDelete(){
mapper.delete(9);
}

@Test
//注解根据id查询
public void testFindById(){
Users users = mapper.findById(2);
System.out.println(users);
}

@Test
//注解查询全部
public void testFindAll(){
List<Users> all = mapper.findAll();
for (Users users : all) {
System.out.println(users);
}
}
}
上一篇:新生研讨课/12月7日/Python编程作业


下一篇:python 之sys.argv