[MyBatis]注解
在抽象接口编辑注解
@Select("select * from user")
List<User> getUsers();
在mybatis-config.xml即mybatis核心配置文件中绑定接口
<!--绑定接口--> <mappers> <mapper class="com.zlc.dao.UserMapper"/> </mappers>
注意:简单sql语句推荐使用注解,复杂sql语句使用xml配置文件方式
目录结构
接口
package com.zlc.dao; import com.zlc.pojo.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper { @Select("select * from user") List<User> getUsers(); }
实体类
package com.zlc.pojo; import org.apache.ibatis.type.Alias; //实体类 @Alias("user") public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
工具类
package com.zlc.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; //sqlSessionFactory-->sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { //第一步:获取sqlSessionFactory对象 String resource="mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //SqlSession完全包含了面向数据库执行SQL命令的所有方法 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
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核心配置文件--> <configuration> <!--引入外部配置文件--> <properties resource="db.properties"/> <!--日志工厂 STDOUT_LOGGING --> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <!--属性别名--> <!--<typeAliases> <typeAlias type="com.User" alias="User"/> </typeAliases>--> <typeAliases> <package name="com.zlc.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <!--数据库--> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--绑定接口--> <mappers> <mapper class="com.zlc.dao.UserMapper"/> </mappers> </configuration>
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8 username=root password=364624