Mybatis之基于XML和注解方式的开发应用专题
文章目录
一、编码流程
- 编写全局配置文件:SqlMapConfig.xml
- 映射文件:xxxMapper.xml
- 编写dao代码:xxxDao接口、xxxDaoImpl实现类
- POJO类
- 单元测试类
二、需求
- 根据用户ID查询一个用户信息
- 根据用户名称模糊查询用户信息列表
- 添加用户
三、项目搭建
3.1创建maven工程:mybatis-demo![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5de32af72bb08200110d39a21cbd0262.png#pic_center)
3.2 POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatiscourse</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
3.3 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>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
</configuration>
3.4 UserMapper.xml
配置说明 1. parameterType:定义输入参数的java类型 2.resultType:定义结果映射类型
3.#{}:相当于JDBC中的?占位符
<?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="findUserById" parameterType="int"
resultType="com.lyf.pojo.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
3.5 PO类
package com.lyf.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private int id;
private String userName;
private Date birthday;
private String sex;
private String address;
}
3.6 dao接口和实现类
package com.lyf.dao;
import com.lyf.pojo.User;
public interface UserDao {
public User findUserById(int id) throws Exception;
}
3.7 实现类
- sqlsession:方法级别
- sqlsessionFactory:全局范围(应用级别)
- sqlsessionFactoryBuilder:方法级别
package com.lyf.dao;
import com.lyf.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class UserDaoImpl implements UserDao{
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory = sqlSessionFactory;
}
public User findUserById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = null;
try{
user = sqlSession.selectOne("test.findUserById",id);
System.out.println(user);
}finally {
sqlSession.close();
}
return user;
}
}
3.7 主键返回
<?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="findUserById" parameterType="int"
resultType="com.lyf.pojo.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="com.lyf.pojo.User">
<!-- selectKey将主键返回,需要再返回 -->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address});
</insert>
</mapper>
添加selectkey标签实现主键返回
- keyProperty:指定返回的主键,存储在POJO中的哪个属性
- order:selectKey标签中的sql执行顺序,是相对于insert语句来说的。由于mysql的自增原理,执行完Insert语句之后才将主键生成,所以这里selectKey的执行顺序为after.
- resultType:返回的转对应java类型
- LAST_INSERT_ID():是mysql的函数,返回auto——increment自增列新纪录ID值
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用步骤
1.引入库
在这里插入代码片
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。