Created
June 20, 2018 13:43
-
-
Save coolcoolercool/274ba54d260b2949c4df929539382291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.itheima.dao; | |
| import cn.itheima.pojo.User; | |
| import java.util.List; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/20 19:36 | |
| */ | |
| public interface UserDao { | |
| public User findUserById(Integer id); | |
| public List<User> findUserName(String userName); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.itheima.dao; | |
| import cn.itheima.pojo.User; | |
| import org.apache.ibatis.session.SqlSession; | |
| import org.apache.ibatis.session.SqlSessionFactory; | |
| import java.util.List; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/20 19:37 | |
| */ | |
| public class UserDaoImpl implements UserDao { | |
| private SqlSessionFactory sqlSessionFactory; | |
| //通过构造方法注入 | |
| public UserDaoImpl(SqlSessionFactory sqlSessionFactory){ | |
| this.sqlSessionFactory = sqlSessionFactory; | |
| } | |
| @Override | |
| public User findUserById(Integer id) { | |
| //sqlSession是线程不安全的,所以他的最佳使用范围在方法体内 | |
| SqlSession openSession = sqlSessionFactory.openSession(); | |
| User user = openSession.selectOne("test.findUserId", id); | |
| return user; | |
| } | |
| @Override | |
| public List<User> findUserName(String userName) { | |
| SqlSession openSession = sqlSessionFactory.openSession(); | |
| List<User> list = openSession.selectList("test.findUserByUserName", userName); | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.itheima.mapper; | |
| import cn.itheima.pojo.User; | |
| import java.util.List; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/20 19:42 | |
| */ | |
| public interface UserMapper { | |
| public User findUserById(Integer id); | |
| //动态代理形势中,如果返回结果集List,那么mybatis | |
| //会在生成实现类的使用会自动调用slectList方法 | |
| public List<User> findUserByUserName(String userName); | |
| public void insertUser(User user); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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接口代理实现编写规则: | |
| 1. 映射文件中namespace要等于接口的全路径名称 | |
| 2. 映射文件中sql语句id要等于接口的方法名称 | |
| 3. 映射文件中传入参数类型要等于接口方法的传入参数类型 | |
| 4. 映射文件中返回结果集类型要等于接口方法的返回值类型 | |
| --> | |
| <mapper namespace="cn.itheima.mapper.UserMapper"> | |
| <!-- | |
| id:sql语句唯一标识 | |
| parameType:指定传入参数类型 | |
| resultType:返回结果集类型 | |
| #{}占位符:起到占位作用, | |
| 如果传入的是基本类型(string,long,double,int,boolean,float等),那么#{}中的变量名称可以随意写. | |
| --> | |
| <select id="findUserById" parameterType="int" resultType="cn.itheima.pojo.User"> | |
| select * from user where id =#{id} | |
| </select> | |
| <!-- | |
| 如果返回结果为集合,可以调用selectList方法,这个方法返回的结果就是一个集合,所以映射文件中应该 | |
| 配置成集合泛型的类型 | |
| ${}拼接符:字符串原样拼接,如果传入的参数是基本类型(String,long,double,int ,boolean,float等), | |
| 那么${}中的变量名称必须是value | |
| 注意:拼接符有sql注入的风险,所以谨慎使用 | |
| --> | |
| <select id="findUserByUserName" parameterType="string" resultType="user"> | |
| select * from user where username like '%${value}' | |
| </select> | |
| <!-- | |
| 执行select LAST_INSERT_ID() 数据库函数,返回自增的主键 | |
| keyProperty:价格返回的主键放入传入参数的id中保存 | |
| order:当前函数相对与insert语句的执行顺序,在insert之前执行的是before,在insert后执行的AFTER | |
| resultType:id的类型,也就是keyproperty中属性的类型 | |
| --> | |
| <insert id="insertUser" parameterType="cn.itheima.pojo.User"> | |
| <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> | |
| select LAST_INSERT_ID() | |
| </selectKey> | |
| insert into user (username,birthday,sex,address) values(#{username}, #{birthday},#{sex}, #{address}) | |
| </insert> | |
| </mapper> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.itheima.pojo; | |
| import java.util.Date; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/20 19:33 | |
| */ | |
| public class User { | |
| private int id; | |
| private String username; //用户姓名 | |
| private String sex; //性别 | |
| private Date birthday; //生日 | |
| private String address; //地址 | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getUsername() { | |
| return username; | |
| } | |
| public void setUsername(String username) { | |
| this.username = username; | |
| } | |
| public String getSex() { | |
| return sex; | |
| } | |
| public void setSex(String sex) { | |
| this.sex = sex; | |
| } | |
| public Date getBirthday() { | |
| return birthday; | |
| } | |
| public void setBirthday(Date birthday) { | |
| this.birthday = birthday; | |
| } | |
| public String getAddress() { | |
| return address; | |
| } | |
| public void setAddress(String address) { | |
| this.address = address; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.itheima.test; | |
| import cn.itheima.pojo.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.junit.Test; | |
| import java.io.InputStream; | |
| import java.util.List; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/20 21:05 | |
| */ | |
| public class UserTest { | |
| @Test | |
| public void testUserById() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| //通过流将核心配置文件读取进来 | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| //通过核心配置文件输入流来创建会话工厂 | |
| SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| //通过工厂创建会话 | |
| SqlSession openSession = factory.openSession(); | |
| //第一个参数;所调用的sql语句 = namespace +...+sql的ID | |
| User user = openSession.selectOne("test.findUserById", 1); | |
| System.out.println(user); | |
| openSession.close(); | |
| } | |
| @Test | |
| public void testFindUserByUserName() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| SqlSession openSession = factory.openSession(); | |
| List<User> list = openSession.selectList("test.findUserByUserName", "王"); | |
| System.out.println(list); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment