mybatis入门(实现增删改查)

目录

  1.查询数据库中id=1的记录

2.通过用户名模糊查询记录

3.插入数据

4.根据id修改用户名

5.根据id删除记录

6.使用mybatis开发dao层(查询id为1的记录。对比上面的)


新建javaProject 

导入数据库驱动包:

导入mybatis包和它的依赖包:

配置约束:https://blog.csdn.net/qq_40323256/article/details/89705297

项目文件如下:

           数据库:

 

全局配置(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>
	<!-- mybatis的环境配置,此信息在开发中只需照搬即可 -->
<!-- 	在集成spring中是不用这种方式的 -->
	<environments default="development">
		<environment id="development">
			<!-- 配置JDBC事务,此事务由mybatis管理 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 配置连接池,此连接池为mybatis连接池 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/web01"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="mapper/UserMapper.xml"/>

<!-- 下面这种方式推荐 -->
	<!-- 	<package name="mapper"/>-->
	</mappers>
	
</configuration>

注意:SqlMapConfig.xml中还可以使用读取配置文件(db.properties)的方式连接数据库,如下:

<?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="db.properties"></properties>

	<!-- mybatis的环境配置,此信息在开发中只需照搬即可 -->
<!-- 	在集成spring中是不用这种方式的 -->
	<environments default="development">
		<environment id="development">
			<!-- 配置JDBC事务,此事务由mybatis管理 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 配置连接池,此连接池为mybatis连接池 -->
			<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>
		<mapper resource="mapper/UserMapper.xml"/>
<!-- 下面这种方式推荐 -->
	<!-- 	<package name="mapper"/>-->
	</mappers>
	
</configuration>

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web01
jdbc.username=root
jdbc.password=root

  1.查询数据库中id=1的记录

UserMapper.xml:

<?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="UserMapper">

	<select id="selectUserById" parameterType="Integer" resultType="com.sikiedu.bean.User">
		SELECT * FROM user WHERE id =#{id}
	</select>
</mapper>

User.java:

package com.sikiedu.bean;

public class User {
	private Integer id;
	private String username;
	private String password;
	
	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;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
	
}

HelloMyBatis.java:

package test;

import java.io.IOException;
import java.io.InputStream;

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.Test;

import com.sikiedu.bean.User;

public class HelloMyBatis {

	@Test
	public void Test() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user = session.selectOne("UserMapper.selectUserById",1);
		System.out.println(user);
	}
}

 运行结果:

2.通过用户名模糊查询记录

HelloBatis.java:

	@Test
	public void Test2() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		List<User> list = session.selectList("UserMapper.selectUserByName", "李");
		for (User u : list) {
			System.out.println(u);
		}
	}

UserMapper.xml:

<!-- 	${}字符串拼接  这里不推荐 -->
<!-- 	#{}占位符 推荐 -->
	<select id="selectUserByName" parameterType="String" resultType="com.sikiedu.bean.User">
<!-- 		SELECT * FROM user WHERE username like '%${value}%' -->
			SELECT * FROM user WHERE username like "%"#{name}"%"
	</select>

运行结果:

3.插入数据

HelloMyBatis.java:

	@Test
	public void Test3() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user=new User();
		user.setUsername("小明");
		user.setPassword("1");
		
		session.insert("UserMapper.insertUser",user);
		session.commit();
	}

UserMapper.xml:

	<insert id="insertUser" parameterType="com.sikiedu.bean.User">
		insert into user values(null,#{username},#{password})
	</insert>

运行结果:

4.根据id修改用户名

HelloMyBatis.java:

	@Test
	public void Test4() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		User user=new User();
		user.setId(14);
		user.setUsername("小疆");
		
		session.insert("UserMapper.updateUser",user);
		session.commit();
	}

UserMapper.xml:

	<update id="updateUser" parameterType="com.sikiedu.bean.User">
		update user set username=#{username} where id=#{id}
	</update>

运行结果:

5.根据id删除记录

HelloMyBatis.java:

	@Test
	public void Test5() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		session.delete("UserMapper.deleteUser",14);
		session.commit();
	}

UserMapper.xml:

	<delete id="deleteUser" parameterType="Integer">
		delete from user where id=#{id}
	</delete>

运行结果:id=14的记录被删除了

6.使用mybatis开发dao层(查询id为1的记录。对比上面的)

UserDao.java:

package com.sikiedu.dao;

import com.sikiedu.bean.User;

public interface UserDao {

		User getUserById(Integer id);
	
}

UserDaolmpl.java:

package com.sikiedu.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.sikiedu.bean.User;

public class UserDaolmpl implements UserDao {
	SqlSessionFactory ssf;

	
	public UserDaolmpl(SqlSessionFactory ssf) {
		super();
		this.ssf = ssf;
	}


	@Override
	public User getUserById(Integer id) {
		SqlSession session  = ssf.openSession();
		return session.selectOne("UserMapper.selectUserById",id);
	}

}

UserDaoTest.java:

package test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.sikiedu.bean.User;
import com.sikiedu.dao.UserDao;
import com.sikiedu.dao.UserDaolmpl;

public class UserDaoTest {

	private static SqlSessionFactory ssf;
	
	static{
		String resource="sqlMapConfig.xml";
		InputStream in;
		try {
			in = Resources.getResourceAsStream(resource);
			SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
			ssf=ssfb.build(in);
			in.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void DaoTest(){
		UserDao dao=new UserDaolmpl(ssf);
		User user=dao.getUserById(1);
		System.out.println(user);
	}
}

运行结果:

7.包装类

UserVo.java:

package com.sikiedu.bean;

public class UserVo {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}

UserMapper.java:

	public User selectUserByUserVo(UserVo vo);

 UserMapper.xml: 

	<select id="selectUserByUserVo" parameterType="com.sikiedu.bean.UserVo" resultType="com.sikiedu.bean.User">
		SELECT * FROM user WHERE id =#{user.id}
	</select>

MapperTest.java:

	@Test
	public void Test3() throws IOException{
		String resource="sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
		SqlSessionFactory ssf=ssfb.build(in);
		SqlSession session  = ssf.openSession();
		
		UserMapper mapper = session.getMapper(UserMapper.class);
		UserVo vo=new UserVo();
		User u=new User();
		u.setId(1);
		vo.setUser(u);
		
		User user = mapper.selectUserByUserVo(vo);
		System.out.println(user);
	}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值