1. 程式人生 > 其它 >java——mybatis——Mybatis註解開發——@Insert——更新資料

java——mybatis——Mybatis註解開發——@Insert——更新資料

pom:

<?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>org.example</groupId> <artifactId>sunxl</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </
properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId
>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j</artifactId> <version>2.17.2</version> </dependency> </dependencies> </project>

工程配置:

<?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>

    <typeAliases>
        <package name="com.sunxl.pojo"/>
    </typeAliases>
    <!-- 配置mybatis執行環境 -->
    <environments default="development">
        <environment id="development">
            <!-- 使用JDBC的事務管理 -->
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <!-- MySQL資料庫驅動 -->
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <!-- 連線資料庫的URL -->
                <property name="url" value="jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <!-- 將mapper檔案加入到配置檔案中 -->
    <mappers>
        <package name="com.sunxl.dao"></package>
    </mappers>
</configuration>

POJO類:

package com.sunxl.pojo;

public class User {

    Integer id;
    String name;
    Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

DAO介面:

package com.sunxl.dao;

import com.sunxl.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {

    @Select("select * from users")
    List<User> findAll();

    @Insert("insert into users (name,age) values(#{name},#{age})")
    void saveUser(User user);


//
//    int updateUser(User user);
//
//    int deleteUser(Integer id);
//
//    User findById(Integer id);
//
//    List<User> findByName(String name);
//
//    int findTotal();
//
//    int saveInsertUser(User user);

}

測試執行:

import com.sunxl.dao.UserDao;
import com.sunxl.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest {

    public static void main(String[] args) throws IOException {

//        InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
//
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//        SqlSessionFactory factory = builder.build(config);
//        SqlSession ss = factory.openSession();
//        UserDao userdao = ss.getMapper(UserDao.class);
//
//
//        List<User> users = userdao.findAll();
//
//        for (User user: users){
//            System.out.println(user);
//        }
//
//
//        ss.commit();
//        ss.close();
//        config.close();



        InputStream config = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(config);
        SqlSession ss = factory.openSession();
        UserDao userdao = ss.getMapper(UserDao.class);

        User as = new User();
        as.setName("zhon_guo");
        as.setAge(1003);

        userdao.saveUser(as);



        ss.commit();
        ss.close();
        config.close();


    }
}

PS:

@Insert("insert into users (name,age) values(#{name},#{age})")
void saveUser(User user);


如果反正型別是int,則返回修改了幾天資料的值

@Insert("insert into users (name,age) values(#{name},#{age})")
int saveUser(User user);