Created
June 21, 2018 06:55
-
-
Save coolcoolercool/e7a0eca535cf0a15446783e5a1b31d7c to your computer and use it in GitHub Desktop.
mubtis
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> findUserByUserName(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.findUserById", id); | |
| return user; | |
| } | |
| @Override | |
| public List<User> findUserByUserName(String userName) { | |
| SqlSession openSession = sqlSessionFactory.openSession(); | |
| List<User> list = openSession.selectList("test.findUserByUserName", userName); | |
| return list; | |
| } | |
| } |
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.dao.UserDao; | |
| import cn.itheima.dao.UserDaoImpl; | |
| import cn.itheima.pojo.User; | |
| import org.apache.ibatis.io.Resources; | |
| import org.apache.ibatis.session.SqlSessionFactory; | |
| import org.apache.ibatis.session.SqlSessionFactoryBuilder; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import java.io.InputStream; | |
| import java.util.List; | |
| /** | |
| * author: zzw5005 | |
| * date: 2018/6/21 14:33 | |
| */ | |
| public class UserDaoTest { | |
| private SqlSessionFactory factory; | |
| //作用:在测试方法前执行这个方法 | |
| @Before | |
| public void setUp() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| //通过流将核心配置文件读取进来 | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| //通过核心配置文件输入流来创建会话工厂 | |
| factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| } | |
| @Test | |
| public void testFindUserById() throws Exception{ | |
| //将初始化好的工厂注入到实现类中 | |
| UserDao userDao = new UserDaoImpl(factory); | |
| User user = userDao.findUserById(1); | |
| System.out.println(user); | |
| } | |
| @Test | |
| public void testFindUserByUserName() throws Exception{ | |
| UserDao userDao = new UserDaoImpl(factory); | |
| List<User> list = userDao.findUserByUserName("王"); | |
| System.out.println(list); | |
| } | |
| } | |
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.Date; | |
| 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); | |
| } | |
| @Test | |
| public void testInsertUser() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| //通过流将核心配置文件读取进来 | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| //通过核心配置文件输入流来创建会话工厂 | |
| SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| //通过工厂创建会话 | |
| SqlSession openSession = factory.openSession(); | |
| User user = new User(); | |
| user.setUsername("赵四"); | |
| user.setBirthday(new Date()); | |
| user.setSex("1"); | |
| user.setAddress("北京海淀"); | |
| System.out.println("=======" + user.getId()); | |
| openSession.insert("test.insertUser", user); | |
| //提交事务(mybatis会自动开启事务,但是它不知道何时提交他,所以需要手动提交) | |
| openSession.commit(); | |
| System.out.println("=======" + user.getId()); | |
| } | |
| @Test | |
| public void testDelUserById() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| //通过流将核心配置文件读取进来 | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| //通过核心配置文件输入流来创建会话工厂 | |
| SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| //通过工厂创建会话 | |
| SqlSession openSession = factory.openSession(); | |
| openSession.delete("test.delUserById", 29); | |
| //提交 | |
| openSession.commit(); | |
| } | |
| @Test | |
| public void testUpdateUserById() throws Exception{ | |
| String resource = "SqlMapConfig.xml"; | |
| //通过流将核心配置我呢见读取进来 | |
| InputStream inputStream = Resources.getResourceAsStream(resource); | |
| //通过核心配置文件输入流来创建会话工厂 | |
| SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); | |
| //通过工厂创建会话 | |
| SqlSession openSession = factory.openSession(); | |
| User user = new User(); | |
| user.setId(27); | |
| user.setUsername("王麻子"); | |
| openSession.update("test.updateUserById", user); | |
| //提交 | |
| openSession.commit(); | |
| } | |
| } | |
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
| jdbc.driver=com.mysql.jdbc.Driver | |
| jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8 | |
| jdbc.username=root | |
| jdbc.password=root |
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
| # Global logging configuration | |
| log4j.rootLogger=DEBUG, stdout | |
| # Console output... | |
| log4j.appender.stdout=org.apache.log4j.ConsoleAppender | |
| log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | |
| log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n |
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 configuration | |
| PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | |
| "http://mybatis.org/dtd/mybatis-3-config.dtd"> | |
| <configuration> | |
| <properties resource="db.properties"></properties> | |
| <typeAliases> | |
| <!-- 定义单个pojo类别名 | |
| type:类的全路劲名称 | |
| alias:别名 | |
| --> | |
| <!--<typeAlias type="cn.itheima.pojo.User" alias="user"/> --> | |
| <!-- 使用包扫描的方式批量定义别名 | |
| 定以后别名等于类名,不区分大小写,但是建议按照java命名规则来,首字母小写,以后每个单词的首字母大写 | |
| --> | |
| <package name="cn.itheima.pojo"/> | |
| </typeAliases> | |
| <!-- 和spring整合后 environments配置将废除--> | |
| <environments default="development"> | |
| <environment id="development"> | |
| <!-- 使用jdbc事务管理--> | |
| <transactionManager type="JDBC" /> | |
| <!-- 数据库连接池--> | |
| <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="User.xml"/> | |
| <!-- | |
| 使用class属性引入接口的全路径名称: | |
| 使用规则: | |
| 1. 接口的名称和映射文件名称除扩展名外要完全相同 | |
| 2. 接口和映射文件要放在同一个目录下 | |
| --> | |
| <!-- <mapper class="cn.itheima.mapper.UserMapper"/> --> | |
| <!-- 使用包扫描的方式批量引入Mapper接口 | |
| 使用规则: | |
| 1. 接口的名称和映射文件名称除扩展名外要完全相同 | |
| 2. 接口和映射文件要放在同一个目录下 | |
| --> | |
| <package name="cn.itheima.mapper"/> | |
| </mappers> | |
| </configuration> |
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"> | |
| <!--namespace:命名空间,做sql隔离--> | |
| <mapper namespace="test"> | |
| <!-- | |
| id:sql语句唯一标识 | |
| parameterType:指定传入参数类型 | |
| resultType:返回结果集类型 | |
| #{}占位符:起到占位作用,如果传入的是基本类型(string,long,double,int,boolean,float等),那么#{}中的变量名称可以随意写. | |
| --> | |
| <select id="findUserById" parameterType="java.lang.Integer" 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="java.lang.String" resultType="cn.itheima.pojo.User"> | |
| select * from user where username like '%${value}%' | |
| </select> | |
| <!-- | |
| #{}:如果传入的是pojo类型,那么#{}中的变量名称必须是pojo中对应的属性.属性.属性..... | |
| 如果要返回数据库自增主键:可以使用select LAST_INSERT_ID() | |
| --> | |
| <insert id="insertUser" parameterType="cn.itheima.pojo.User" > | |
| <!-- 执行 select LAST_INSERT_ID()数据库函数,返回自增的主键 | |
| keyProperty:将返回的主键放入传入参数的Id中保存. | |
| order:当前函数相对于insert语句的执行顺序,在insert前执行是before,在insert后执行是AFTER | |
| resultType:id的类型,也就是keyproperties中属性的类型 | |
| --> | |
| <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> | |
| <delete id="delUserById" parameterType="int"> | |
| delete from user where id=#{id} | |
| </delete> | |
| <update id="updateUserById" parameterType="cn.itheima.pojo.User"> | |
| update user set username=#{username} where id=#{id} | |
| </update> | |
| </mapper> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment