Mybatis的基于注解的增删改查操作

1.mybatis配置文件:

<?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>
   <!--- 配置环境-->
    <environments default="mysql">
        <!---配置mysql的环境-->
        <environment id="mysql">
            <!--- 配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--- 配置数据源(连接池)-->
            <dataSource type="POOLED">
            <!--- 配置连接数据库的4个基本信息-->
                <!--property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///test1?characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>  < 方式二  直接配置-->
            </dataSource>
        </environment>
    </environments>
<!---指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
    <mappers>
        <mapper class="com.xatu.dao.IUserDao"></mapper>   <!--select注释方式 -->
    </mappers>
</configuration>

关于url中&useSSL=false&serverTimezone=Asia/Shanghai 这一部分是由于我的mysql数据库版本是8.0.16;如果不加这些会报错。

2.User类

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String 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 Date getBirthday() {
        return birthday;
    }

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

    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;
    }

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

注意此类中的变量名要与数据库中变量的名字一一对应

3.操作类:Dao接口

public interface IUserDao {
    /*
    查询所有操作
     */
     @Select("select * from user")
    List<User> findAll();

    /**
     * 保存用户
     * @param user
     */
     @Select("insert into user values(#{username},#{birthday},#{sex},#{address})")
    void saveUser(User user);

    /**
     * 删除用户
     */
    @Select("delete from user where id=47")
    void delUser();

    /**
     * 查询操作
     * @return
     */
    @Select("select * from user where id=46")
    User selectUser();

    /**
     * 更新操作
     */
    @Select("update user set username=#{username} where id=46")
    void updateUser(User user);
}

4.测试类:

public class MybatisTest2 {
    static SqlSession sqlSession=null;
    static IUserDao dao=null;
    static InputStream is=null;
    @Before
    public void init() throws IOException {
        is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(is);

        sqlSession = sqlSessionFactory.openSession();
        dao = sqlSession.getMapper(IUserDao.class);
    }
    @After
    public void destory() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }

    @Test
    
    public void testupdate() {        //更新操作
        User us=new User();
        us.setUsername("小马宝莉");
        dao.updateUser(us);
    }
    @Test
    public void testSelectAll(){   //查询所有
    	List<User> users = dao.findAll();
    	for (User user :
           	 users) {
            System.out.println(user);
    	}  
    }
    @Test
    public void deleteUser(){  //删除操作
 	dao.delUser();
    }
    @Test
    public void saveUser(){
    	User user = dao.selectUser();
	System.out.println(user);
    }
}

你可能感兴趣的:(SSM框架)