前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:首先盗个图。
一、使用步骤
1.引入库
首先需要引入Maven依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.创建配置文件
然后需要编写mybatis-config.xml 文件
不用各位手写,以及为大家准备好了。cv 即可,开箱即用
<?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>
<!-- 全局setting配置,根据需要添加 -->
<!-- 配置别名 -->
<typeAliases>
<!-- 批量扫描别名 -->
</typeAliases>
<typeHandlers>
<!-- <package name="com.hh.userservice.handle"/>-->
<package name="com.hh.userservice.handle.ExampleTypeHandle"/>
</typeHandlers>
<!-- 由于使用spring和mybatis的整合包进行dataSource的设置整合,这里不需要配置了,可用于junit单元测试 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置mappper
如果使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。可用于junit单元测试
-->
<mappers>
<mapper resource="\mappers\UserMapper.xml"/>
</mappers>
</configuration>
标注:
<mapper resource="\mappers\UserMapper.xml"/>
切记使用 “\”否者不能读取到配置文件。
如果还是不能读取到配置文件的时候同时可以在pom.xml文件中添加如下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
3.创建实例对象
package com.hh.userservice.pojo;
import com.hh.userservice.handle.ExampleTypeHandle;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.annotation.ColumnType;
import java.io.Serializable;
import java.util.Set;
@Data
@Accessors(chain = true)
@Component
@NoArgsConstructor
@AllArgsConstructor
public class UserBean implements Serializable {
public static final long serialVersionUID = 1L;
private String id;
@ColumnType(jdbcType = JdbcType.VARCHAR, typeHandler = ExampleTypeHandle.class)
private String username;
private String password;
private String email;
private String telephone;
private Set<Role> roles;
}
3.Mapper接口映射
package com.hh.userservice.mapper;
import com.hh.userservice.pojo.UserBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapper {
UserBean findUserBeanById(@Param("id") String id);
UserBean findUserBeanByUserName(@Param("username") String username);
void insertUserBeanData(UserBean userBean);
}
4.XML配置文件编写
提示:namespace需要填写正确,防止出现无法绑定等异常。
<?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="com.hh.userservice.mapper.UserMapper">
<resultMap id="mapper" type="com.hh.userservice.pojo.UserBean">
<id column="id" property="id"></id>
<result column="username" property="username" typeHandler="com.hh.userservice.handle.ExampleTypeHandle"></result>
<result column="password" property="password"></result>
<result column="email" property="email"></result>
<result column="telephone" property="telephone"></result>
</resultMap>
<select id="findUserBeanById" resultMap="mapper">
select * from u_user where id = #{id}
</select>
<select id="findUserBeanByUserName" resultMap="mapper">
select * from u_user where username = #{username,typeHandler=com.hh.userservice.handle.ExampleTypeHandle}
</select>
<select id="insertUserBeanData">
insert into u_user(id,username,password,email,telephone) value(#{id},#{username,typeHandler=com.hh.userservice.handle.ExampleTypeHandle},#{password}
,#{email},#{telephone})
</select>
</mapper>
3.编写单元测试
保存数据的时候切记如果需要手动提交的话,需要在sqlSession中的commit方法添加一个true的参数。还有另外一种方式通过sqlSessionFactory 工厂创建SqlSession 的时候传递一个自动提交的状态。
@Test
public void insertMybatisUserData() throws Exception{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE,true); // 通过工厂创建SqlSession的同时自动提交事务。
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
UserBean bean = new UserBean(UUID.randomUUID().toString().replaceAll("-","")
,"lisi","123456","994036938@qq.com","17806525487",null);
mapper.insertUserBeanData(bean);
sqlSession.commit(true); // 需要手动提交的时候需要传入一个参数,如果不传递参数的话能提交成功,但是不能保存数据到数据库里。
}
Mybatis通过主键获取数据
@Test
public void selectMybatisData() throws Exception{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
UserBean userBean = mapper.findUserBeanById("a7fa3b5d5fae11eb830db42e99446dea");
UserBean userBean1 = mapper.findUserBeanById("a7fa3b5d5fae11eb830db42e99446dea");
System.out.println(userBean);
System.out.println(userBean1);
}
多说一下:
Mybatis中有三种Executor执行器贴一个源码:
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? this.defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Object executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (this.cacheEnabled) {
executor = new CachingExecutor((Executor)executor);
}
Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
return executor;
}
总结
人生悟语:
当你能看懂一件事情的时候,说明你长大了;当你能看清一件事情的时候,说明你开窍了;当你能看破一件事情的时候,说明你理性了;当你能看透一件事情的时候,说明你成熟了;当你能看淡一件事情的时候,说明你放下了。