首页 技术 正文
技术 2022年11月20日
0 收藏 505 点赞 4,908 浏览 8158 个字

 注意:我们在resultType中,对于selectlist方法也是projo类。resultType参数的含义是list的泛型的类型。

一:jar包下载:

https://github.com/mybatis/mybatis-3/releases?after=mybatis-3.2.8

我使用的版本是3.2,7

JAVA框架  Mybaits

二、创建项目 导入核心jar包和依赖的jar包。

pom.xml文件内容:

   <properties>
<!--需要注意还珠格格编译器的版本要和jdk版本一直-->
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.16.-GA</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

mybaits的配置文件说明:

JAVA框架  Mybaits

其中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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池 mybaits自带的连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/day_spring?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers >
<mapper resource="account.xml" />
</mappers>
</configuration>

account.xml是子配置文件。包含一些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="test1"><!--namespace起到sql隔离的作用-->
<select id="FindAccountById" resultType="jd.com.mybaitstest.account" parameterType="java.lang.Integer" >
SELECT * FROM t_account WHERE id=#{id};
</select>
<!--注意like拼接的需要使用#{value} 必须是value。-->
<select id="FindUsernameByNanme" resultType="jd.com.mybaitstest.account" parameterType="java.lang.String" > SELECT * FROM t_account WHERE NAME LIKE '%${value}%';
</select>
<!--插入语句 注意 这里我们插入值是account对象 注意的是这里的占位符的名字是account的属性--> <insert id="InsertVal" parameterType="jd.com.mybaitstest.account">
<!-- id返回的设置 标签selectkey 属性keyproperty表示:account的属性名,order 获取是插入的前(before)还是后(after)返回的数据类型-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
<!--通过数据库的内置函数SELECT LAST_INSERT_ID()获取插入的语句返回的id的值。mybaits帮我们设置到我们对象中。并设置属性是id-->
SELECT LAST_INSERT_ID();
</selectKey>
INSERT INTO t_account(NAME ,money) VALUES(#{name},#{money});
</insert>
<delete id="DeletAccoutnById" parameterType="int" >
DELETE FROM t_account WHERE id=#{id};
</delete>
<update id="UpdateById" parameterType="jd.com.mybaitstest.account" >
UPDATE t_account SET money=#{money} WHERE id=#{id};
</update>
</mapper>

mybaits的简单的增删改查:

 package jd.com.mybaitstest; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class testmyba { @Test
public void testdemo1() throws IOException {
//查询单条数据
String resouce="SqlMapConfig.xml";
InputStream inputStream= Resources.getResourceAsStream(resouce);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
account ac=sqlSession.selectOne("test1.FindAccountById",);
System.out.println(ac);
}
@Test
public void testdemo2() throws IOException {
String resource="SqlMapConfig.xml";
//获取主配置文件流
InputStream inp=Resources.getResourceAsStream(resource);
//创建数据库回话
SqlSession sqlSession=new SqlSessionFactoryBuilder().build(inp).openSession();
List<account> list=sqlSession.selectList("test1.FindUsernameByNanme","ok");
System.out.println(list); }
@Test
public void testInsertdemo() throws IOException {
String resource="SqlMapConfig.xml";
InputStream inp=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inp);
SqlSession sqlSession=sqlSessionFactory.openSession();
account ac=new account();
ac.setMoney();
ac.setName("oko");
sqlSession.insert("test1.InsertVal",ac);
//在主配置文件中配置了事务。需要commit 才能生效。
sqlSession.commit();
System.out.println(ac.getId());
}
@Test
public void deleteDemo() throws IOException {
String resource="SqlMapConfig.xml";
InputStream inp=Resources.getResourceAsStream(resource);
SqlSession sqlSession=new SqlSessionFactoryBuilder().build(inp).openSession();
sqlSession.delete("test1.DeletAccoutnById",);
sqlSession.commit();
}
@Test
public void updateById() throws IOException {
String resource="SqlMapConfig.xml";
InputStream inp=Resources.getResourceAsStream(resource);
SqlSession sqlSession=new SqlSessionFactoryBuilder().build(inp).openSession();
account ac=new account();
ac.setMoney();
ac.setId();
sqlSession.update("test1.UpdateById",ac);
sqlSession.commit();
}
}

注意:我们使用sqlsessionfactory来获取sqlsession,其中通过selelct、delete、update、insert等方法进行进行增删改查。

其中方法内的参数delete(“子配置文件的中namespace.语句id值”,参数)。也就是说我们 通过子配置文件的namespace值和语句中的id值来唯一确定执行那个sql语句。

子配置文件的占位符:

占位符:作用是底层调用jdbc的时候,使用preparestatment的时候,进行sql的占位符一一对应,传入参数。

#{}占位符:

  如果传入的是基本的类型那么#{}中的变量名字可以随便写。

  如果传入的是pojo类的时候,那么#{}的变量名字必须要和pojo类的属性名字一致。

${}拼接符:

  如果传入的是基本的类型那么${}中的变量名称必须为value。

  如果传入的是pojo类的时候,那么${}必须是和pojo的类的属性名称一致。

  注意${}拼接符,有可能造成sql注入,解决方法可以在用户输入的页面进行校验不可输入sql关键字,不可以输入空格。或者后端做校验验证。

主键返回

  主键一般是自增或者使用uuid。通过数据库的内置函数获取插入值之后的主键值。

1、uuid:

 <insert  id="insertUser" parameterType="cn.itcast.mybatis.po.User"> <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id"> select uuid() </selectKey> insert into user(id,username,birthday,sex,address)        values(#{id},#{username},#{birthday},#{sex},#{address}) </insert>

注意这里使用的order是“BEFORE”,因为uuid需要我们提前生成 在插入数据库所以在insert语句之前执行。

2、自增主键:

     <insert id="InsertVal" parameterType="jd.com.mybaitstest.account">
<!-- id返回的设置 标签selectkey 属性keyproperty表示:account的属性名,order 获取是插入的前(before)还是后(after)返回的数据类型-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
<!--通过数据库的内置函数SELECT LAST_INSERT_ID()获取插入的语句返回的id的值。mybaits帮我们设置到我们对象中。并设置属性是id-->
SELECT LAST_INSERT_ID();
</selectKey>
INSERT INTO t_account(NAME ,money) VALUES(#{name},#{money});
</insert>

三、原生dao层编写:

dao层:

interface:

 package jd.com.dao; import jd.com.mybaitstest.account; public interface acci {
void selecDemo(int i);
void updateDemo(account ac);
}

implement class:

 package jd.com.dao; import jd.com.Utils.session;
import jd.com.mybaitstest.account;
import org.apache.ibatis.session.SqlSession; public class acciImpl implements acci {
@Override
public void selecDemo(int i) {
SqlSession sqlSession=session.getSession();
account ac=sqlSession.selectOne("test1.FindAccountById",i);
System.out.println(ac);
} @Override
public void updateDemo(account ac) {
SqlSession sqlSession=session.getSession();
sqlSession.update("test1.UpdateById",ac);
sqlSession.commit();
}
}

测试程序:

 package jd.com.dao; import jd.com.mybaitstest.account;
import org.junit.jupiter.api.Test; public class testdemodap { @Test
public void testdemo1(){
acciImpl ac1=new acciImpl();
ac1.selecDemo();
account ac=new account();
ac.setMoney();
ac.setName("poppp");
ac.setId();
ac1.updateDemo(ac);
}
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,967
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783