pom文件要加两个依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
新建Resource文件–>新建两个配置文件mybatis.cfg.xml和local.xml
mybatis.cfg.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>
<!-- 如参数属性是一个类,可以给长的类名取别名 -->
<typeAliases>
<typeAlias type="com.bdqn.entity.Local" alias="local"></typeAlias>
</typeAliases>
<environments default="zjy">
<environment id="zjy">
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.181.129:3306/myshops"/>
<property name="username" value="root"/>
<property name="password" value="kb10"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--关联两个配置文件-->
<mapper resource="mappers/local.xml"></mapper>
</mappers>
</configuration>
local.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="com.bdnq.dao.localDAO">
<insert id="insertLocal" parameterType="local">
insert into local values(#{localid},#{localname},#{sublocal})
</insert>
<delete id="deleteLocal" parameterType="int">
delete from local where localid=#{localid}
</delete>
<update id="updateLocal" parameterType="local">
update local set localname=#{localname} ,sublocal=#{sublocal} where localid=#{lcoalid}
</update>
<select id="findAll" resultType="local">
select * from local
</select>
<select id="findById" parameterType="int" resultType="local">
select * from local where localid=#{localid}
</select>
</mapper>
设置parameterType:防止有同名的重载方法
JAVA实体类:
package com.bdqn.entity;
public class Local {
private int localid;
private String localname;
private int sublocal;
public int getLocalid() {
return localid;
}
public void setLocalid(int localid) {
this.localid = localid;
}
public String getLocalname() {
return localname;
}
public void setLocalname(String localname) {
this.localname = localname;
}
public int getSublocal() {
return sublocal;
}
public void setSublocal(int sublocal) {
this.sublocal = sublocal;
}
}
接口
package com.bdnq.dao;
import com.bdqn.entity.Local;
import java.util.List;
public interface localDAO {
public void insertLocal(Local local);
public void deleteLocal(int localid);
public void updateLocal(Local local);
public List<Local> findAll();
public Local findById(int localid);
}
测试类:
package com.bdqn.entity;
import com.bdnq.dao.localDAO;
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 java.io.IOException;
import java.io.Reader;
public class MyTest {
public static void main(String[] args) throws IOException {
//读取配置文件
Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
//根据配置获得数据库操作会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
//根据操作工厂生产操作会话
SqlSession session = factory.openSession();
//根据会话生成对应的数据库操作实例
localDAO ldao = session.getMapper(localDAO.class);
//操作接口,测试效果
// System.out.println(ldao.findAll());
// System.out.println(ldao.findById(2).getLocalname());
//开启事务,添加数据
Local loc = new Local();
// loc.setLocalid(21);
// loc.setLocalname("扬州");
// loc.setSublocal(2);
// ldao.insertLocal(loc);
// session.commit();
//修改数据
// loc.setLocalid(21);
// loc.setLocalname("南通");
// loc.setSublocal(2);
// ldao.updateLocal(loc);
//删除数据
ldao.deleteLocal(21);
session.commit();
//关闭接口
session.close();
}
}