标签:driver comm http image repos 简介 mes com details
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录.
搭建一个Maven项目**
在IDEA上不会搭建Maven的参考
[IDEA的简单入门](https://blog.csdn.net/qwqweizai/article/details/106557022
配置pom.xml的环境依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
创建一个mysql测试表。()
连接IDEA上的databases
5.在resources下配置mybatis-config.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>
<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://127.0.0.1:3306/mybatis?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lgw/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
创建sqlSession**
建议放在自己untils工具包下
public class MybatisUntils {
private static SqlSessionFactory sqlSessionFactory =null;
// 1.获取sqlSessionFactory工厂
static{
String resource ="mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//2. 获取 SqlSession对象
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
// 3.测试
public static void main(String[] args) {
SqlSession sqlSession=MybatisUntils.getSqlSession();
System.out.println("sqlSession="+sqlSession);
}
}
创建实体类
建议放在pojo包下
根据自己对应的数据库创建实体类(我的get.set,tosting,构造方法这里省略)
public class User {
private int id;
private String username;
private String password;
}
创建UserMapper接口
建议放在Mapper目录下
这是我的UserMapper接口,方法可以根据你的实际情况定义
public interface UserMapper {
/** 获取全部用户
* @return User的List集合
*/
List<User> getUserList();
/** 通过Id获取到用户
* @param id 编号
* @return User对象
*/
User getUserById(Integer id);
/**
* @param map map集合
* @return User
*/
User getUserByIdMap(Map<String,Object>map);
/**
*
* @param key 模糊查询关键字
* @return List<User>
*/
List<User> getUserByLike(String key);
/** 增加用户
* @param user user对象
* @return 影响几条
*/
int addUser(User user);
/** 更新用户
* @param user user对象
* @return 影响几条
*/
int updateUser(User user);
/** 更新用户
* @param id id
* @return 影响几条
*/
int deleteUser(Integer id);
}
创建UserMapper.xml
我这里是用 XML 来配置的
如果你把xml文件放置在(java的)Mapper目录下,在pom.xml加入
<build>
<!--设置后就可以读取java目录下的xml跟.properties-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
UserMapper.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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="getUserById" resultType="com.lgw.pojo.User" parameterType="java.lang.Integer">
select *from user where id=#{id}
</select>
<!--利用map传参-->
<select id="getUserByIdMap" resultType="com.lgw.pojo.User" parameterType="map">
select *from user where id=#{id} and username=#{name}
</select>
<!--模糊查询-->
<!-- select *from user where password like "%"#{key}"%" -->
<select id="getUserByLike" resultType="com.lgw.pojo.User" parameterType="java.lang.String">
select *from user where password like "%"#{key}"%"
</select>
<insert id="addUser" parameterType="com.lgw.pojo.User">
insert into Mybatis.user(id,username,password) values(#{id},#{username},#{password})
</insert>
<update id="updateUser" parameterType="com.lgw.pojo.User">
update Mybatis.user set username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from Mybatis.user where id=#{id}
</delete>
</mapper>
在mybatis-config.xml配置文件下加入扫描器
<mappers>
<mapper resource="com/lgw/dao/UserMapper.xml"></mapper>
</mappers>
resource对应UserMapper.xml路径(注意是用 / , 不是用 . )
在test目录下创建(com.***.Mapper)包(建议)
查询测试
@Test
public void QueryUser(){
SqlSession session=null;
try {
//1.获取工厂
session=MybatisUntils.getSqlSession();
// 2.执行sql
UserMapper userMapper=session.getMapper(UserMapper.class);
//
List<User> userList=userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
// User user=userMapper.getUserById(2);
// System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
// 3.关流
session.close();
}
}
增,删,改的话要提交事物
//增 删 改 需要提交事物
@Test
public void addUser(){
//1.获取工厂
SqlSession session=MybatisUntils.getSqlSession();
// 2.执行sql
UserMapper userMapper=session.getMapper(UserMapper.class);
int i=userMapper.addUser(new User(3,"mohu","chaxun"));
if(i>0){
System.out.println("插入数据成功");
}
//3.提交事物
session.commit();
session.close();
}
模糊查询
@Test
public void QueryUserByLike(){
SqlSession session=MybatisUntils.getSqlSession();
UserMapper UserMapper=session.getMapper(UserMapper.class);
List<User> userList=UserMapper.getUserByLike("112");
for (User user : userList) {
System.out.println(user);
}
session.close();
}
@Test
public void QueryUserByMap(){
SqlSession session=MybatisUntils.getSqlSession();
UserMapper UserMapper=session.getMapper(UserMapper.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",4);
map.put("name","update123");
User user=UserMapper.getUserByIdMap(map);
System.out.println("user="+user);
session.close();
}
这样基本上就完成Mybatis的增删查改。达到Mybatis入门
标签:driver comm http image repos 简介 mes com details
原文地址:https://www.cnblogs.com/sky1023/p/13052969.html