mybatis入门案例
pom.xml,mybatis.xml,db.properties,UserDao.xml文件配置 pom.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 > org.example</groupId > <artifactId > MybatisTest</artifactId > <version > 1.0-SNAPSHOT</version > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > </properties > <repositories > <repository > <id > public</id > <name > localprivatenexus</name > <url > http://maven.aliyun.com/nexus/content/groups/public/</url > <releases > <enabled > true</enabled > </releases > </repository > </repositories > <pluginRepositories > <pluginRepository > <id > public</id > <name > localprivatenexus</name > <url > http://maven.aliyun.com/nexus/content/groups/public/</url > <releases > <enabled > true</enabled > </releases > <snapshots > <enabled > false</enabled > </snapshots > </pluginRepository > </pluginRepositories > <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 > 5.1.6</version > </dependency > </dependencies > <build > <resources > <resource > <directory > src/main/java</directory > <includes > <include > **/*.xml</include > </includes > <filtering > true</filtering > </resource > </resources > </build > </project >
mybatis.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 ="db.properties" /> <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${jdbc.driver}" /> <property name ="url" value ="${jdbc.url}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="Dao\UserDao.xml" /> </mappers > </configuration >
UserDao.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 ="Dao.UserDao" > <select id ="userList" resultType ="entity.User" > select id,username,password from test01 order by id </select > <insert id ="insertUser" > insert into test01 values (#{id},#{username},#{password}) </insert > <delete id ="deleteUser" > delete from test01 where id=#{id} </delete > </mapper >
db.properties
jdbc.driver =com.mysql.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3307/mybatis?useUnicode=true&characterEncoding=utf-8 jdbc.username =root jdbc.password =33333333
创建实体类和dao层 entity.User
package entity;public class User { private String username,password; private int id; public int getId () { return id; } public void setId (int id) { this .id = id; } public String getUsername () { return username; } public void setUsername (String username) { this .username = username; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } public User (String username, String password, int id) { this .username = username; this .password = password; this .id = id; } public User () { } @Override public String toString () { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", id=" + id + '}' ; } }
Dao.UserDao
package Dao;import entity.User;import java.io.IOException;import java.util.List;public interface UserDao { public List<User> userList () throws IOException ; public int insertUser (User user) throws IOException ; public int deleteUser (User user) throws IOException ; }
实现类
package Dao;import entity.User;import org.apache.ibatis.session.SqlSession;import utils.MybatisUtils;import java.io.IOException;import java.util.List;public class UserDaoImpl implements UserDao { @Override public List<User> userList () throws IOException { SqlSession sqlSession= MybatisUtils.getSqlSession(); String sqlId="Dao.UserDao." +"userList" ; return sqlSession.selectList(sqlId); } @Override public int insertUser (User user) throws IOException { SqlSession sqlSession=MybatisUtils.getSqlSession(); String sqlId="Dao.UserDao." +"insertUser" ; return sqlSession.insert(sqlId,user); } @Override public int deleteUser (User user) throws IOException { SqlSession sqlSession=MybatisUtils.getSqlSession(); String sqlId="Dao.UserDao." +"deleteUser" ; return sqlSession.delete(sqlId,user.getId()); } }
重复步骤则封装成工具类 MybatisUtils
package utils;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.InputStream;public class MybatisUtils { public static SqlSessionFactory sqlSessionFactory=null ; static { String config="mybatis.xml" ; try { InputStream in= Resources.getResourceAsStream(config); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); sqlSessionFactory=builder.build(in); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession () throws IOException { if (sqlSessionFactory != null ) { return sqlSessionFactory.openSession(true ); } return null ; } }
测试类 import Dao.UserDao;import Dao.UserDaoImpl;import entity.User;import org.apache.ibatis.session.SqlSession;import utils.MybatisUtils;import java.io.IOException;import java.util.List;public class Test { public static void main (String[] args) throws IOException { UserDao userDao=new UserDaoImpl(); userDao.userList().forEach(user -> System.out.println(user)); } }
看看是否能查出来~~
完美!! 下一篇 为:利用jdk动态代理来简化查询过程,即省略手动实现类