好久没在csdn发博客,找工作可真费劲,今天来写一篇mybatis的博客
1. 环境搭建
过于懒惰,直接Maven吧
<dependencies>
<!--导入数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--导入mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--导入junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2. 目录结构
Mybatis所需要的配置文件主要有mybatis-config.xml和xxxMapper.xml
database.properties放置连接数据库所需要的参数
Person是我们的Bean
PersonDao是我们的接口,实现是我们的xxxMapper.xml文件
测试都放在MyTest类中
3. 代码及配置文件
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>
<!--用来加载数据库连接参数信息-->
<properties resource="database.properties"></properties>
<!--起别名使用,我怕出错,比较喜欢用准确的名字,别名就不演示了-->
<typeAliases>
<package name=""/>
</typeAliases>
<!--指定使用哪个配置环境-->
<environments default="development">
<!--id作为唯一标识-->
<environment id="development">
<!--事务管理类型为jdbc-->
<transactionManager type="JDBC"></transactionManager>
<!--数据源类型为pooled-->
<dataSource type="POOLED">
<!--指定数据库用户名,密码,数据库驱动,数据库地址-->
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载mapper文件-->
<mapper resource="Mapper/PersonDaoMapper.xml"></mapper>
</mappers>
</configuration>
database.properties
database.url=jdbc:mysql://localhost:3306/lyb?useSSL=false&serverTimezone=UTC
database.driver=com.mysql.cj.jdbc.Driver
database.username=root
database.password=123456
PersonDaoMapper.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="com.ccut.Dao.PersonDao">
<select id="selectById" parameterType="Integer" resultType="com.ccut.Dao.Person">
SELECT id,NAME,age,address,cid FROM testmybatis WHERE id=#{id}
</select>
<insert id="insert" parameterType="com.ccut.Dao.Person" useGeneratedKeys="true" keyProperty="id" >
INSERT INTO testmybatis(NAME,age,address,cid) VALUE(#{name},#{age},#{address},#{cid})
</insert>
<delete id="delete" parameterType="Integer">
DELETE FROM testmybatis WHERE id=#{id}
</delete>
<update id="update" parameterType="com.ccut.Dao.Person" >
UPDATE testmybatis SET NAME=#{name},age=#{age},address=#{address},cid=#{cid} WHERE id=#{id}
</update>
</mapper>
Person
package com.ccut.Dao;
public class Person {
private Integer id;
private String name;
private Integer age;
private String address;
private Integer cid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Person() {
}
public Person(Integer id, String name, Integer age, String address, Integer cid) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.cid = cid;
}
public Person(String name, Integer age, String address, Integer cid) {
this.name = name;
this.age = age;
this.address = address;
this.cid = cid;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", cid=" + cid +
'}';
}
}
PersonDao
package com.ccut.Dao;
public interface PersonDao {
public Person selectById(Integer id);
public int update(Person person);
public int delete(Integer id);
public int insert(Person person);
}
MyTest
package com.ccut.Test;
import com.ccut.Dao.Person;
import com.ccut.Dao.PersonDao;
import com.ccut.Util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class MyTest {
SqlSession sqlSession= MybatisUtil.getSqlSession();
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
@Test
public void selectById(){
Person person = personDao.selectById(1);
System.out.println(person);
}
@Test
public void insert(){
Person person=new Person("bb",18,"吉林长春",2);
int n=personDao.insert(person);
sqlSession.commit();
if(n>0){
System.out.println("插入成功");
}
else {
System.out.println("插入失败");
}
}
@Test
public void delete(){
int n=personDao.delete(21);
sqlSession.commit();
if(n>0){
System.out.println("删除成功");
}
else {
System.out.println("删除失败");
}
}
@Test
public void update(){
Person person=new Person(19,"root",19,"jilin",3);
int n=personDao.update(person);
sqlSession.commit();
if(n>0){
System.out.println("修改成功");
}
else {
System.out.println("修改失败");
}
}
}
到此再分享下自己的模板吧
怎么打开这个不用多说了吧
直接上模板
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>
<properties resource=""></properties>
<typeAliases>
<package name=""/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=""></mapper>
</mappers>
</configuration>
database.properties
database.url=jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=UTC
database.driver=com.mysql.cj.jdbc.Driver
database.username=用户名
database.password=密码
这个主要是针对mysql数据库的,有余力补全orcle的
xxxMapper.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="实现类的全路径">
</mapper>
这个就没啥含量了
有更好的模板可以推荐给我,为了快速编码值得啊