1.创建JDBC
2.实体类
3.创建接口(类名必须和SQL.XML的类名相同)
4.创建SQL.XML
5.执行代码
1.创建JDBC
<?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>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<!-- 给返回值起别名 默认类名 -->
<package name="com.gx.pojo"/>
<!-- 给返回值起别名可以 自定义名称 -->
<!-- <typeAlias type="com.gx.pojo.Y_YuangGong" alias="自定义"/> -->
</typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybas"/>
<property name="username" value="root"/>
<property name="password" value="scott"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 创建连接 -->
<mapper resource="com/gx/dao/LogMapper.xml"/>
</mappers>
</configuration>
2.实体类
public class Account {
private int accountID;
private String num;
private String password;
private double balance;
private String name;
public int getAccountID() {
return accountID;
}
public void setAccountID(int accountID) {
this.accountID = accountID;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Account [accountID=" + accountID + ", num=" + num
+ ", password=" + password + ", balance=" + balance + ", name="
+ name + "]";
}
}
3.创建接口
public interface LogMapper {
List<Account> selectAccount();
List<Account> selectAccountByNumPass(String num ,String password);
//它是把参数转成Map集合,@Param("nun")是键-nun111是值;
//使用在sql.xml中写键来取值,和nun111命名无关;
List<Account> selectAccountByNumPassOne(@Param("nun") String nun111,@Param("password") String password111);
}
方法名称必须和sql.xml的id对应;
4.创建SQL.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">
<!--LogMapper必须和接口类目相同-->
<mapper namespace="com.gx.dao.LogMapper">
<!--id必须对应接口方法名称-->
<select id="selectAccount" resultType="Account">
select * from account
</select>
<!--不需要参数类型-->
<select id="selectAccountByNumPass" resultType="Account">
select * from account where num=#{0} and password=#{1}
<!-- select * from Y_YuangGong where YuanGongDaiMa=#{Param1} and MiMa=#{Param2} -->
</select>
<!--跟@param("")的命名相同-->
<select id="selectAccountByNumPassOne" resultType="Account">
select * from account where num=#{nun} and password=#{password}
</select>
</mapper>
5.执行代码
public static void main(String[] args) throws IOException {
//数据库连接属性
InputStream resourceAsStream = Resources.getResourceAsStream("GlobalConfiguration.xml");
//连接工厂
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//打开数据库
SqlSession openSession = build.openSession();
//实例接口
LogMapper mapper = openSession.getMapper(LogMapper.class);
//通过实体接口直接调取出接口里面的方法
//查询所有
List<Account> selectAccount = mapper.selectAccount();
for (Account account : selectAccount) {
System.out.println(account.toString());
}
//条件查询
List<Account> selectAccountByNumPass = mapper.selectAccountByNumPass("1", "1");
for (Account account : selectAccountByNumPass) {
System.out.println(account.toString());
}
//指定参数名
List<Account> selectAccountByNumPassOne = mapper.selectAccountByNumPassOne("2", "2");
for (Account account : selectAccountByNumPassOne) {
System.out.println(selectAccountByNumPassOne.toString());
}
}
注意:
1.创建接口类名和SQL.XML类名必须相同;(public interface LogMapper 和 namespace="com.gx.dao.LogMapper")
2.接口方法名称和对应需要使用的SQL语句的ID相同;( id="selectAccount" 相同 List<Account> selectAccount();)