说实话,自从疫情在家待了那么久,闲也是闲,天天打游戏也打了一个多月,感觉没多大意义,然后想想自己也快毕业了,也没学到多少,感觉也是有些紧迫感。所以呀,生为学生还是得学,以学为主,今天趁周末跟楠哥学习MyBatis:楠哥教你学Java 没有废话,极简入门,安利了安利了。
作为一名程序员,为了能看懂自己的笔记,先上个代码目录:
接下来就放出跟着楠哥学习MyBatis所构建的文件的代码。第一个视频跟着楠哥学习了使用MyBatis对MySQL数据库进行了insert 数据。
package com. southwind. entity;
import lombok. Data;
@Data
public class Account {
private int id;
private String username;
private String password;
private int age;
public Account ( int id, String username, String password, int age) {
this . id = id;
this . username = username;
this . password = password;
this . age = age;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}' ;
}
}
1.这是用原生接口的方法
接下来是创建一个XML文件,用来映射数据库,对数据库进行操作
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
< mapper namespace = " com.southwind.mapper.AccountMapper" >
< insert id = " save" parameterType = " com.southwind.entity.Account" >
insert into t_account(username,password,age)
values(#{username},#{password},#{age})
</ insert>
</ mapper>
这个XML文件是用来连接数据库 并且注册声明对数据库进行操作的某个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" > </ transactionManager>
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.cj.jdbc.Driver" />
< property name = " driver" value = " com.mysql.cj.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://xxx:xxx/表名?useUnicode=true& characterEncoding=UTF-8" />
< property name = " username" value = " 数据库用户名" />
< property name = " password" value = " 数据库密码" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " com/southwind/mapper/AccountMapper.xml" > </ mapper>
</ mappers>
</ configuration>
package com. southwind. test;
import com. southwind. entity. Account;
import org. apache. ibatis. session. SqlSession;
import org. apache. ibatis. session. SqlSessionFactory;
import org. apache. ibatis. session. SqlSessionFactoryBuilder;
import java. io. InputStream;
public class Test {
public static void main ( String[ ] args) {
InputStream inputStream = Test. class . getClassLoader ( ) . getResourceAsStream ( "config.xml" ) ;
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ( ) ;
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder. build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
String statement = "com.southwind.mapper.AccountMapper.save" ;
Account account = new Account ( 2 , "oldbai" , "123456" , 22 ) ;
sqlSession. insert ( statement, account) ;
sqlSession. commit ( ) ;
}
}
当然 最最最重要的是导入依赖包的XML文件不可忘记
<?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> com.southwind</ groupId>
< artifactId> aimybatis</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< dependencies>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.4.5</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.19</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.6</ version>
< scope> provided</ scope>
</ dependency>
</ dependencies>
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.xml</ include>
</ includes>
</ resource>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> *.xml</ include>
< include> *.properties</ include>
</ includes>
</ resource>
</ resources>
</ build>
</ project>
最后 运行Test 测试类,我用Navicat 查看数据库,的确插入了新的数据。
2.接下来这是运用Mapper代理接口,共用上面同一个Account类
新加三个文件
AccountRepository 接口:
package com. southwind. repository;
import com. southwind. entity. Account;
import java. util. List;
public interface AccountRepository {
public int save ( Account account) ;
public int update ( Account account) ;
public int deleteById ( int id) ;
public List< Account> findAll ( ) ;
public Account findById ( int id) ;
public Account findByNameAndAge ( String name, int age) ;
}
AccountRepositoryMapper.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
< mapper namespace = " com.southwind.repository.AccountRepository" >
< insert id = " save" parameterType = " com.southwind.entity.Account" >
insert into t_account(username,password,age)
values(#{username},#{password},#{age})
</ insert>
< update id = " update" parameterType = " com.southwind.entity.Account" >
update t_account
set username = #{username},
password = #{password},
age = #{age}
where id = #{id}
</ update>
< delete id = " deleteById" parameterType = " int" >
delete from t_account
where id = #{id}
</ delete>
< select id = " findAll" resultType = " com.southwind.entity.Account" >
select * from t_account
</ select>
< select id = " findById" parameterType = " int" resultType = " com.southwind.entity.Account" >
select * from t_account where id = #{id}
</ select>
< select id = " findByNameAndAge" resultType = " com.southwind.entity.Account" >
select * from t_account where username = #{param1}and age = #{param2}
</ select>
</ mapper>
package com. southwind. test;
import com. southwind. entity. Account;
import com. southwind. repository. AccountRepository;
import org. apache. ibatis. session. SqlSession;
import org. apache. ibatis. session. SqlSessionFactory;
import org. apache. ibatis. session. SqlSessionFactoryBuilder;
import java. io. InputStream;
import java. util. List;
public class Test_2 {
public static void main ( String[ ] args) {
InputStream inputStream = Test. class . getClassLoader ( ) . getResourceAsStream ( "config.xml" ) ;
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ( ) ;
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder. build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
AccountRepository accountRepository = sqlSession. getMapper ( AccountRepository. class ) ;
System. out. println ( accountRepository. findByNameAndAge ( "zhangsan" , 22 ) ) ;
sqlSession. close ( ) ;
}
}
< mapper resource = " com/southwind/repository/AccountRepositoryMapper.xml" > </ mapper>
这就是运行结果啦:
因为之前也用过JPA来做了一个个人博客项目,但是由于没有正确的进行项目策划规划,到后面出现了前端的页面问题,这都是后话,简单对比起来,MyBatis和JPA,我个人觉得都挺好用,如果贪图方便,很方便很方便那种,建议使用JPA;如果自己对SQL语句有一定的基础,建议使用MyBatis。当然对于一名合格的程序员,两种都要掌握那是最好不过。最后再安利一波,楠哥教你学Java 我没打广告,我觉得真的很不错。