标签:ati let 核心 默认 source mil nbsp 查找 mic
一.导包
1.导3类包:mybatis核心包,mybatis依赖包,驱动包
二.设计表,创建表
三.创建实体类
四.创建主配置文件,映射文件
1.主配置文件sqlMapConfig.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> <!-- 和spring整合后 environments配置将废除 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlmap/User.xml"/> </mappers> </configuration>
由于是单独使用mybatis作为演示,所以得在environments标签中配置数据库的参数信息,等到学习spring整合时,就不需要该标签啦
2.映射文件配置sql查询语句
<?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"> <!-- 写Sql语句 --> <mapper namespace="test"> <select id="findUserById" parameterType="Integer" resultType="cn.itheima.mybatis.pojo.User"> select * from user where id = #{v} </select> </mapper> namespace:为访问指定sql配置标签的前缀;
select标签的id:起到标识的作用;
parameterType:传入参数的类型
resultType:返回结果集自动映射的类型,前提是保证表列名跟类属性名一致
#{}:代表占位符,括号里面任意
五.junit测试按ID查询
public class JunitTest { @Test
//按ID查询 public void test1() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流,默认从classpath路径下查找 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 User user = session.selectOne("test.findUserById", 10); System.out.println(user); } catch (IOException e) { e.printStackTrace(); } } }
由于主配置文件的命名规范没有做硬性要求,所以我们需要拿到该配置文件的流对象或者其他形式的对象;不同于hibernate的主配置文件的位置和命名做了限制,在获取时不需手动指定配置文件路径!
六.按名字模糊查询
@Test //模糊查询 public void test2() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 List<User> list = session.selectList("test.findUserByName", "五"); System.out.println(list); } catch (IOException e) { e.printStackTrace(); } }
<mapper namespace="test"> <select id="findUserById" parameterType="Integer" resultType="cn.itheima.mybatis.pojo.User"> select * from user where id = #{v} </select> <select id="findUserByName" parameterType="String" resultType="cn.itheima.mybatis.pojo.User"> select * from user where username like "%"#{value}"%" </select> </mapper>
这里resultType不是List<User>,当返回结果集为多行记录时,resultType代表集合的泛型
普及一下:#{ }和${ }
七.添加用户
@Test //添加用户 public void test3() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 User user = new User(); user.setUsername("曾志伟"); session.insert("test.insertUser",user); session.commit(); } catch (IOException e) { e.printStackTrace(); } }
<insert id="insertUser" parameterType="cn.itheima.mybatis.pojo.User">
insert into user (username) values( #{username} )
</insert>
parameterType类型为user,sql语句的占位符不需要写成#{user.username},只要保证占位符里的属性名和传入参数的类的属性名一致即可
对数据库进行增删改(不包括查),需要提交事务才能同步到数据库
八.添加用户返回ID插入到对象
@Test //添加用户返回ID public void test4() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 User user = new User(); user.setUsername("本伟"); session.insert("test.insertUser",user);
//获取Id System.out.println(user.getId()); session.commit(); } catch (IOException e) { e.printStackTrace(); } }
<insert id="insertUser" parameterType="cn.itheima.mybatis.pojo.User">
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into user (username) values( #{username} )
</insert>
order:这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素
select LAST_INSERT_ID():是mysql提供的查询
九.修改用户
@Test //修改用户 public void test5() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 User user = new User(); user.setId(29); user.setUsername("本伟nbbb"); session.update("test.updateByUser", user); session.commit(); } catch (IOException e) { e.printStackTrace(); } }
<updateid="updateByUser" parameterType="cn.itheima.mybatis.pojo.User"> update user set username = #{username} where id = #{id} </update>
十.删除用户
@Test //删除用户 public void test6() { //加载主配置文件 String resource = "sqlMapConfig.xml"; try { //获取指向该配置的流 InputStream in = Resources.getResourceAsStream(resource); //获取工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //获取session SqlSession session = factory.openSession(); //执行sql语句 session.update("test.deleteById", 29); session.commit(); } catch (IOException e) { e.printStackTrace(); } }
<delete id="deleteById" parameterType="Integer"> delete from user where id = #{id} </delete>
标签:ati let 核心 默认 source mil nbsp 查找 mic
原文地址:https://www.cnblogs.com/ibcdwx/p/12924802.html