mybatis实现简单的增删改查
程序员文章站
2022-07-12 22:37:53
...
mybatis实现简单的增删改查
项目结构
添加maven依赖
创建好springboot项目后在pom.xml中添加
// An highlighted block
<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.43</version>
</dependency>
配置mybatis-config.xml
下面展示一些 内联代码片
。
// An highlighted block
<?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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/><!--配置数据库驱动-->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/><!--注意-->
</mappers>
</configuration>
创建UserMapper.xml
// An highlighted block
<?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="Dao.UserMapper">
<select id="selectUser" resultType="Domain.User">
select * from user
</select>
<select id="searchId" resultType="Domain.User">
select * from user where id=#{User_id}
</select>
<insert id="add" parameterType="Domain.User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO `user` ( `Id`, `pwd`, `name`)
VALUES
(#{id},#{pwd},#{name});
</insert>
<update id="update" parameterType="Domain.User">
update user
set
pwd=#{pwd},
name=#{name}
where Id=#{id}
</update>
<delete id="delete" parameterType="Domain.User">
delete from user where Id=#{id}
</delete>
</mapper>
创建dao层并写UserMapper
// An highlighted block
package Dao;
import Domain.User;
import java.util.List;
public interface UserMapper {
//查询所有用户
List<User> selectUser();
//按id查询
User searchId(int id);
//添加用户
void add(User user);
//更改信息
void update(User user);
//删除用户
void delete(User user);
}
创建实体类Domain层并写User
// An highlighted block
package Domain;
public class User {
private int id;
private String pwd;
private String name;
public User(){}
public User(int id,String pwd,String name){
this.id=id;
this.pwd= pwd;
this.name = name;
}
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
在主程序中测试增删改查
// An highlighted block
package com.zhang.firstproject;
import Dao.UserMapper;
import Domain.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.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@SpringBootApplication
public class FirstprojectApplication {
public FirstprojectApplication() throws IOException {
}
public static void main(String[] args) {
SpringApplication.run(FirstprojectApplication.class, args);
}
String resouce = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resouce);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
{
UserMapper usermapper = sqlSession.getMapper(Dao.UserMapper.class);
User user = new User();
user.setPwd("1234");
user.setName("张五");
usermapper.add(user);
sqlSession.commit();//防止插入运行成功,但没有插入数据库中
List<User> alluser = usermapper.selectUser();
System.out.println(alluser.toString());
}
}
创建db数据库,建立user表
运行结果
上一篇: mybatis源码学习------StatementHandler
下一篇: Mybatis学习笔记