实体类
package cn.liwen.entity;public class User { private long id; //主键 private String username;//用户名 private String password;//密码 public long getId() { return id; } public void setId(long 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 + '\'' + '}'; }}
接口实现类
package cn.liwen.impl;import cn.liwen.entity.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.testng.annotations.Test;import org.testng.annotations.BeforeClass;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.List;import java.util.Map;public class UserDaoImpl { private SqlSession session = null; @BeforeClass public void init() throws IOException { // 1、启动mybatis框架 // SqlSession--->SqlSessionFatory----->SqlSessionFatoryBuilder SqlSessionFactoryBuilder ssb = new SqlSessionFactoryBuilder(); // 将mybatis.xml文件转化成流 InputStream ins = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory ssf = ssb.build(ins); session = ssf.openSession(); } /** * 查询所以名字 * */ @Test public void getAllUser() { /** 2、调用局部配置文件中的sql语句*/ List<User> userList = session.selectList("cn.liwen.impl.UserDaoImpl.getAllUser"); for (User user : userList) { System.out.println(user); } } /**使用lists方式*/ @Test public void getAllUserlist() throws IOException { List<User> userList = session.selectList("cn.liwen.impl.UserDaoImpl.getAllUserlist"); for (User user : userList) { System.out.println(user); } } /**使用map方式*/ @Test public void getAllUser2() { // 2、调用局部配置文件中的sql语句 List<Map<String, Object>> userList = session.selectList("cn.liwen.impl.UserDaoImpl.getAllUser2"); for (Map<String, Object> map : userList) { System.out.println(map); } } /**测试单条记录方式*/ @Test public void getUserById() { //带参数方式 long id = 1L; Map<String, Object> userList = session.selectOne("cn.liwen.impl.UserDaoImpl.getUserById",id); System.out.println(userList); } /**多个参数*/ @Test public void getUserByCondition() { Map<String,Object> paraMap = new HashMap<String, Object>(); paraMap.put("username","李玟"); paraMap.put("pwd","321321"); Map<String, Object> userList = session.selectOne("cn.liwen.impl.UserDaoImpl.getUserById",paraMap); System.out.println(userList); } /**通过实体类方式取值*/ @Test public void getUserByCondition2() { // 同时传递多个参数 User user = new User(); user.setUsername("李玟"); user.setPassword("321321"); Map<String, Object> userMap = session.selectOne("cn.liwen.impl.UserDaoImpl.getUserByCondition2", user); System.out.println(userMap); } /** 执行模糊查询 */ @Test public void getUserByMh() { // 传递参数 Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("password", "3"); List<Map<String, Object>> userMap = session.selectList("cn.liwen.impl.UserDaoImpl.getUserByMh", paramMap); for (Map<String, Object> map : userMap) { System.out.println(map); } } /**添加数据 delete、insert、update标签没有resultType属性 */ @Test public void addUser() { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("username","北京"); paramMap.put("password","123455"); int flag = session.insert("cn.liwen.impl.UserDaoImpl.addUser", paramMap); //增删改必须提交事务 session.commit(); System.out.println(flag); }}
接口实现类.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 namespace="cn.liwen.impl.UserDaoImpl"> <!-- sql语句保存在Mybatis的局部配置文件中 --> <!-- select标签存放查询语句(List<User>) id:在整个配置文件中id值必须唯一,其值与dao层类中的方法名保持一致 resultType:指定当前sql查询语句返回的数据类型。类型不是为sql语句的最终类型,而是某一条数据的类型 --> <!-- findallusr : 是别名 --> <select id="getAllUser" resultType="findallusr"> SELECT * FROM users </select> <!--使用list方式调用 --> <select id="getAllUserlist" resultType="findallusr"> SELECT * FROM users </select> <!-- 使用map 方法查询--> <select id="getAllUser2" resultType="Map"> SELECT * FROM users </select> <!-- parameterType:指定接收参数类型 --> <select id="getUserById" resultType="Map" parameterType="long"> SELECT * FROM users where id=#{id} </select> <!-- 当传递的参数有多个条件时 --> <select id="getUserByCondition" resultType="map" parameterType="Map"> SELECT * FROM users WHERE username=#{username} AND PASSWORD=#{pwd} </select> <!-- 当传递的参数有多个条件时 通过实体类方式 --> <select id="getUserByCondition2" resultType="map" parameterType="cn.liwen.entity.User"> SELECT * FROM users WHERE username=#{username} AND PASSWORD=#{password} </select> <!-- 执行模糊查询 --> <select id="getUserByMh" resultType="map" parameterType="map"> SELECT * FROM users WHERE PASSWORD LIKE CONCAT('%',#{password},'%') </select> <!-- 添加数据 delete、insert、update标签没有resultType属性 --> <insert id="addUser" parameterType="Map"> INSERT INTO users SET username=#{username},PASSWORD=#{password} </insert></mapper>
mybatis.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> <!-- 别名 --> <typeAliases> <typeAlias type="cn.liwen.entity.User" alias="findallusr"/> </typeAliases> <!-- 配置数据库连接环境:driver、url、username、password default===默认配置 --> <environments default="mysql"> <!-- 开始配置mysql --> <environment id="mysql"> <!--配置事务 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.47.196:3306/test?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 关联局部配置文件 --> <mappers> <mapper resource="cn/liwen/impl/UserDaoImpl.xml"/> </mappers></configuration>
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦