Mybatis实现增删查改管理用户名、密码(附代码)

mybatis核心配置文件



<configuration>
  <environments default="mysql8">
    <environment id="mysql8">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="190211"/>
      dataSource>
    environment>
  environments>
  
  <mappers>
    <mapper resource="UsersMapper.xml"/>
  mappers>
configuration>

SQL映射文件



<mapper namespace="com.sdau.mybatis.UsersMapper">
  <select id="selectone" resultType="com.sdau.mybatisexp.entities.Users">
    select * from Users where username = #{username}
  select>
  <insert id="insertOneRecord" parameterType="com.sdau.mybatisexp.entities.Users">
  	insert into users values(#{username},#{userpwd})
  insert>
  <update id="updateByUsername" >
  	update users set userpwd=#{userpwd} where username=#{username}
  update>
  <delete id="deleteByUsername">
  	delete from users where username=#{username}
  delete>
mapper>

实体类

package com.sdau.mybatisexp.entities;

public class Users {
	private String username;
	private String userpwd;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpwd() {
		return userpwd;
	}
	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}
}

业务层

package com.sdau.mybatisexp.dao;

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 com.sdau.mybatisexp.entities.Users;

public class UsersDao {
	public Users selectOne(String username) throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		try (SqlSession session = sqlSessionFactory.openSession()) {
			Users us = (Users) session.selectOne("com.sdau.mybatis.UsersMapper.selectone", username);
			return us;
		}
	}
	
	public int insertOne(Users us) throws IOException
	{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		try (SqlSession session = sqlSessionFactory.openSession()) {
			  int n=session.insert("com.sdau.mybatis.UsersMapper.insertOneRecord",us);
			  session.commit();
			  return n;
			}	
	}

	public int updateByName(Users us) throws IOException
	{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		try (SqlSession session = sqlSessionFactory.openSession()) {
			  int n=session.insert("com.sdau.mybatis.UsersMapper.updateByUsername",us);
			  session.commit();
			  return n;
			}			
	}

	  public int deleteByName(String name) throws IOException
		{
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			
			try (SqlSession session = sqlSessionFactory.openSession()) {
				  int n=session.delete("com.sdau.mybatis.UsersMapper.deleteByUsername",name);
				  session.commit();
				  return n;
				}				
		}
}

测试类

package mybatisexp;

import java.io.IOException;
import java.util.Scanner;

import org.junit.Test;

import com.sdau.mybatisexp.dao.UsersDao;
import com.sdau.mybatisexp.entities.Users;

public class TestUsersDao {

	@Test
	public void testUsersDao() throws IOException {
		UsersDao ud = new UsersDao();
		Users us = ud.selectOne("li");
		System.out.println(us.getUsername());
		System.out.println(us.getUserpwd());
	}

	@Test
	public void testInsertOne() throws IOException {
		Scanner sc = new Scanner(System.in);
		String username = sc.next();
		String userpwd = sc.next();
		Users us = new Users();
		us.setUsername(username);
		us.setUserpwd(userpwd);
		UsersDao usersDao = new UsersDao();
		int num = usersDao.insertOne(us);
		System.out.println(num);
		sc.close();
	}

	@Test
	public void testUpdateByName() throws IOException {
		Scanner sc = new Scanner(System.in);
		String username = sc.next();
		String userpwd = sc.next();
		Users us = new Users();
		us.setUsername(username);
		us.setUserpwd(userpwd);
		UsersDao usersDao = new UsersDao();
		int num = usersDao.updateByName(us);
		System.out.println(num);
		sc.close();
	}

	@Test
	public void testDeleteByName() throws IOException {
		int num;
		UsersDao usersDao = new UsersDao();
		num = usersDao.deleteByName("liu");
		System.out.println(num);
	}
}

你可能感兴趣的:(Mybatis,mybatis,mysql)