mybatis基础之一
Posted 笨笨莱鸟学编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis基础之一相关的知识,希望对你有一定的参考价值。
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/spring?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- 加载映射文件 --> <mappers> <mapper resource="sqlmap/Uers.xml"/> </mappers> </configuration>
Users.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命名空间,就是对sql进行分类化管理 理解sql隔离 --> <mapper namespace="test"> <!-- 在映射文件中配置sql语句 --> <!-- 通过ID查询sql记录 --> <!-- 通过select来执行查询 id: 标识映射文件中的sql 称为statement的id 将sql语句封装到mappedStatement中 parameterType:指定输入参数的类型 ,这里指定int型 #{}表示一个占位符 #{id} 表示接收输入的参数 id就是输入的参数名称 resultTypeL: 指定sql输出结果的javadui类型 --> <select id="findUserById" parameterType="int" resultType="com.liu.mybatis.po.User"> SELECT * FROM USER WHERE id = #{id} </select> <!-- ${}是一个拼接符号 --> <select id="findUserByName" parameterType="java.lang.String" resultType="com.liu.mybatis.po.User"> SELECT * FROM USER WHERE username like ‘%${value}%‘ </select> <!-- selectkey获取自增主键 --> <insert id="insertUser" parameterType="com.liu.mybatis.po.User"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) </insert> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> <update id="updateUser" parameterType="com.liu.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update> </mapper>
User.java
package com.liu.mybatis.po;
import java.util.Date;
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;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
MybatisFirst.java
package com.liu.mybatis.first; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; 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 com.liu.mybatis.po.User; public class MybatisFirst { @Test public void findUserByIdTest() throws IOException{ //得到mybatis配置文件 String resource = "SqlMapConfig.xml"; //得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //通过sqlSession操作数据库 User user = sqlSession.selectOne("test.findUserById", 1); System.out.println(user); List<User> list = sqlSession.selectList("test.findUserByName", "明"); System.out.println(list); User user2 = new User(); user2.setId(28); user2.setUsername("王晓春三个混蛋"); user2.setBirthday(new Date()); user2.setSex("男"); user2.setAddress("安微合肥"); /*sqlSession.insert("test.insertUser", user2);*/ sqlSession.update("test.updateUser", user2); /*sqlSession.delete("test.deleteUser", user2);*/ sqlSession.commit(); sqlSession.close(); } }
以上是关于mybatis基础之一的主要内容,如果未能解决你的问题,请参考以下文章