关于c#:Mybatis配置数据库连接

59次阅读

共计 4183 个字符,预计需要花费 11 分钟才能阅读完成。

首先 resources 下的 mybatis-config.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssms?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/zmxy/dao/UsersMapper.xml"/>
    </mappers>
</configuration>

pojo 实现类层的 Java 类 (对应数据库表), 比方 Users 表

public class Users {
  private int id;
  private String name;
  private String pwd;

  public Users(){}

}

dao 层的映射 xml 和 Interface 接口

UsersMapper.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">
<!--namespace 命名空间 保定一个对应的 dao/mapper 接口 -->
<mapper namespace="cn.zmxy.dao.UsersMapper">
 <select id="getUserList" resultType="cn.zmxy.pojo.Users">
  SELECT * FROM ssms.users
    </select>
 <select id="getUserById" resultType="cn.zmxy.pojo.Users" parameterType="int">
  SELECT * FROM ssms.users WHERE id = #{id}
  </select>
 <insert id="addUser" parameterType="cn.zmxy.pojo.Users">
  INSERT INTO ssms.users (id, name, pwd) VALUES (#{id}, #{name}, #{pwd})
    </insert>
 <update id="updateUser" parameterType="cn.zmxy.pojo.Users">
  UPDATE ssms.users SET name = #{name}, pwd = #{pwd} WHERE id = #{id}
  </update>
 <delete id="deleteUser" parameterType="int">
  DELETE FROM ssms.users WHERE id = #{id}
  </delete>
</mapper>

UsersMapper.java

package cn.zmxy.dao;
import cn.zmxy.pojo.Users;
import java.util.List;

public interface UsersMapper {

  // 查问全副用户
  public List<Users> getUserList();

  // 依据 ID 查问用户
  public Users getUserById(int id);

  // 增加用户
  public int addUser(Users user);

  // 批改用户
  public int updateUser(Users user);

  // 删除用户
  public int deleteUser(int id);
}

写一个 utils 类,获取 SqlSession

MyBatisUtils.java

package cn.zmxy.utils;

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;

public class MyBatisUtils {

  private static SqlSessionFactory sqlSessionFactory;

  static {
  try {
  String resource = "mybatis-config.xml";
  InputStream is = Resources.getResourceAsStream(resource);
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
 } catch (IOException e) {e.printStackTrace();
 } }
  public static SqlSession getSqlSession()
 {return sqlSessionFactory.openSession();
 }}

最初就能够调用了,写一个 test 类

UserMapperTest.java

package cn.zmxy.dao;
import cn.zmxy.pojo.Users;
import cn.zmxy.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;

public class UserDaoTest {
  @Test
  public void test()
 {  // 第一步,获取 SqlSession 对象
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  // 第二步,执行 SQL
  UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
  List<Users> userList = usersMapper.getUserList();

  // 打印 users
  for (Users users : userList) {System.out.println(users);
 }  // 敞开 SqlSession
  sqlSession.close();}
  @Test
  public void getUserById()
 {  // 第一步,获取 SqlSession 对象
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  Users user = mapper.getUserById(1);
  System.out.println(user);
  // 敞开 SqlSession
  sqlSession.close();}
  @Test
  public void addUser()
 {  // 第一步,获取 SqlSession 对象
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.addUser(new Users(0, "露露", "1234"));
  System.out.println("受影响行数:" + num);
  sqlSession.commit();
  // 敞开 SqlSession
  sqlSession.close();}
  @Test
  public void updateUser()
 {  // 第一步,获取 SqlSession 对象
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.updateUser(new Users(3, "露露", "1234"));
  System.out.println("受影响行数:" + num);
  sqlSession.commit();
  // 敞开 SqlSession
  sqlSession.close();}
  @Test
  public void deleteUser()
 {  // 第一步,获取 SqlSession 对象
  SqlSession sqlSession = MyBatisUtils.getSqlSession();
  UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);

  int num = mapper.deleteUser(11);
  System.out.println("受影响行数:" + num);
  sqlSession.commit();
  // 敞开 SqlSession
  sqlSession.close();}
}

正文完
 0