About Getting mybatis and few notes

mybatis entry procedures are divided into steps:

1. dependence introduction

<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</ dependency>
</ the Dependencies>

2 write configuration file sqlmapconfig.xml (templates can be prepared in advance in the idea of)
<?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 resource="jdbc.properties"></properties> <!--引用外部配置文件 即jdbc.propertioes-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<Property name = "password" value = "$ {jdbc.password}" />
</ the dataSource>
</ Environment>
</ Environments>
<mappers> <-! annotation note in the referenced tag references from mappers mapper-- "becomes Package ->
<Package name =" cn.kgc.dao "/>
</ by mappers>
</ Configuration>
3 Write the entity class
package cn.kgc.domain;

/**
* @Date:Create in 2019/8/4 19:37
*/
public class Users {
private Integer id;
private String username;
private String password;
private String sex;
private String address;

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

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

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}

4 Write dao layer
package cn.kgc.dao;

import cn.kgc.domain.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;

/**
* @Date:Create in 2019/8/4 19:41
*/
public interface UserDao {
//查询全部
@Select("select * from users")
public List<Users> findAll();
//根据id查询
@Select("select * from users where id=#{id}")
public Users findById(Integer id);
//增加一条数据
@Insert("insert into users values(null,#{username},#{password},#{sex},#{address})")
void Save public (the Users User);
// delete data in accordance with an ID
@Delete ( "Delete from Users WHERE ID = # {ID}")
public void Delete (ID Integer);
// modify data
@Update ( "update users set username = # {} username ID WHERE ID = # {} ")
public void Update (the Users User);
}
. 5 write test classes
import cn.kgc.dao.UserDao;
import cn.kgc.domain.Users;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

/**
* @Date:Create in 2019/8/4 20:27
*/
public class TestMybatis {
@Test
public void test(){
//创建SqlSessionFactory工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
System.out.println // (userDao.findAll ());
// System.out.println (userDao.findById (8));
the Users the Users = new new the Users ();
users.setSex ( "F");
the Users. setUsername ( "words");
users.setPassword ( "11111");
users.setAddress ( "Hangzhou");
users.setId (12);
// userDao.save (the Users);
// userDao.update (the Users);
userDao.delete (12);
sqlSession.commit ();
sqlSession.close ();
}
}

in addition:
you must pay attention to when writing sqlmapconfig.xml file, be careful, because one letter a symbol of the project could lead to error, and the general error small causes are extremely hard to find and pay attention to ok as long as written.




Guess you like

Origin www.cnblogs.com/yufudiaodayu/p/11299747.html