java学习之MybBaits
作者:互联网
0x00前言
我前面使用的jdbc和jdbc的工具类集成的但是它们在少部分代码的情况下会会简单,但是以后如果项目较大jdbc的固定代码会很难维护,如果使用框架会简单很多,也标志着java学习正式进入到框架阶段
0x01配置MyBaits
0x1创建模块,导入坐标
在maven的配置文件中pom.xml中加入所需要的jar包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
0x2配置数据库连接文件
修改自己的数据库地址和密码;
<?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>
<package name="com.it.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 数据库连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/tese"/>
<property name="username" value="root"/>
<property name="password" value="zhonglin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
0x3编写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">
<mapper namespace="test">
<select id="findAll" resultType="com.test.domain.User">
select * from user;
</select>
</mapper>
在数据库配置文件中要加入,文件名就是你的sql映射文件
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
0x02入门使用
0x1定义实体类
实体类的内容为你数据库查询的内容
public class User {
private String name;
private String password;
private String gender;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", id=" + id +
'}';
}
0x2基础使用
-
加载核心配置文件
-
获取sqlSession对象,执行SQL语句
-
释放资源
public class demo1 { public static void main(String[] args) throws IOException { //1.加载mybatis配置文件,获取SqlSessionFactory,sql语句工厂 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");//输出流获取配置文件地址 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//获取bulid对象然后获取Factory对象 SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); SqlSession sqlSession = build.openSession(); List<User> users = sqlSession.selectList("test.findAll");//通过sql语句的映射文件去查询 System.out.println(users); sqlSession.close(); } }
0x03代理开发
0x1配置映射文件和接口
定义sql映射文件相同名字的Mapper文件,且文件路径要一致,就是要在同一个文件夹下面
UserMapper文件:
public interface UserMapper {
List<User> findAll();
}
映射文件是在resources下面的所以我们要给它新的一个文件夹他的命名方式是com/test而接口的命名方式是com.test这点是要注意的,随后需要修改
0x2配置核心文件
<mappers>
<mapper resource="com/test/UserMapper.xml"/>
</mappers>
需要把在mybatis-config中的映射声明改成新的文件路径
0x3使用Mapper开发
- 还是先获取配置文件,然后用sqlSession对象获取userMapper.class文件,mapper就是UserMapper的代理对象了
- 用代理对象去实现UserMapper中的方法
public class demo2 {
public static void main(String[] args) throws IOException {
//1.加载mybatis配置文件,获取SqlSessionFactory,sql语句工厂
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
0x04MyBatis的增删改查
0x1条件查询
如果是对同一个表进行的操作就写在一个接口里面
public interface UserMapper {
List<User> findAll();
User selectbyID(int id);
}
在对于的xml文件里面写入对应的sql语句
<mapper namespace="com.test.domain.com.test.UserMapper">
<select id="findAll" resultType="com.test.domain.User">
select * from user;
</select>
<select id="selectbyID" resultType="com.test.domain.User">
select * from user where id =#{id}
</select>
</mapper>
还是用代理的方式进行开发和查询
public class dmo3 {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectbyID(5);
System.out.println(user);
sqlSession.close();
}
}
0x2resultMap标签
<resultMap id="paswwrod" type="com.test.domain.User">
<result column="paswword" property="password"></result>
</resultMap>
这个标签的作用可以用于当你的pojo(实体)对象的属性和你的数据的表明对不上的时候可以做一个属性的映射
- id=唯一表示符合
- type=pojo对象
- property:实体类的属性名
0x3增删改查
学习完这些基础的以后剩下的就是sql代码的问题了列如;模糊查询应该怎么做
1.在对应表的接口写出方法
public interface UserMapper {
List<User> findlike(String name);
}
写出对于的sql语句,模糊查询select *(需要的属性名) from user(对应的表名) where name like #{name}(条件)
<select id="findlike" resultMap="paswwrod">
select * from user where name like #{mame}
</select>
对应的删除方法
public interface UserMapper {
void delById(int id );
}
<delete id="delById">
delete * from user where id=#{id}
</delete>
对应修改方法
void updateId(User user);
<update id="updateId">
UPDATE user set
name =#{name},
gender=#{gender},
password=#{password}
where id=#{id}
</update>
public static void main(String[] args) throws IOException {
User user = new User();
user.setGender("男");
user.setName("小小");
user.setPassword("123452");
user.setId(14);
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateId(user);
对应修改方法
void add(User user);
<insert id="add">
insert into user (name,password,gender)
values (#{name},#{password},#{gender})
</insert>
public static void main(String[] args) throws IOException {
User user = new User();
user.setPassword("root");
user.setName("hellopw");
user.setGender("女");
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.add(user);
sqlSession.close();
#0x05总结
其实现在的MyBatis还有一些负责当它和spring一起使用的时候会更简单,需要记住的是sql语句的语法。
标签:MybBaits,java,String,UserMapper,public,学习,user,id,name 来源: https://www.cnblogs.com/0x3e-time/p/16324209.html