MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.33version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.6version>
- dependency>
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "https://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="${driver}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- dataSource>
- environment>
- environments>
- <mappers>
- <mapper resource="org/mybatis/example/BlogMapper.xml"/>
- mappers>
- configuration>
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="org.mybatis.example.BlogMapper">
- <select id="selectBlog" resultType="Blog">
- select * from Blog where id = #{id}
- select>
- mapper>
在要使用mybatis的类中操作
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
用来调用sql语句操作数据库
SqlSession sqlSession = sqlSessionFactory.openSession();
注意:
* 1.方法的名字与对象sql的id保持一致
* 2.对应xml的namespace值必须是对应接口的类型全名
- List
findAll(); - Employee findById(int id);
- Employee findByIdAndName(Map
map) ; - Employee findByIdAndName(Employee employee);
-
- //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
- Employee findByIdAndName(@Param("id")int id,@Param("name") String name);
-
- //增
- boolean add(Employee employee);
- //改
- void update(@Param("id") int id,@Param("name") String name);
- <mapper namespace="com.woniuxy.mapper.EmployeeMapper">
-
-
- <select id="findByIdAndName" resultType="com.woniuxy.entity.Employee">
- select * from employee where id = #{id} and name = #{name}
- select>
-
- <insert id="add">
- insert into employee values(null,#{cellphone},#{password},#{avatar},#{name},'0');
- insert>
- <update id="update">
- update employee set name = #{name} where id = #{id}
- update>
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
- //5.单参
- Employee byId = mapper.findById(1);
- System.out.println(byId);
-
- //6.多参
- Map
map = new HashMap<>(); - map.put("id",1);
- map.put("name","小李");
- Employee byIdAndName = mapper.findByIdAndName(map);
- System.out.println("map"+byIdAndName);
-
- Employee arg = new Employee();
- arg.setId(1);
- arg.setName("小李");
- Employee byIdAndName1 = mapper.findByIdAndName(arg);
- System.out.println("arg:"+byIdAndName1);
mysql不支持多个参数单独传递,只能一个数据或者多个数据(map、entity、array、list)
- Employee findByIdAndName(Map
map) ; - Employee findByIdAndName(Employee employee);
-
- //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
- Employee findByIdAndName(@Param("id")int id,@Param("name") String name);
实例对象与注解的方法比较好用,map一般不用