您的位置:首页 > 其它

MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现

2017-06-08 23:02 831 查看
使用MyBatis对表执行CRUD操作——基于注解的实现

package me.gacl.mapping;

import java.util.List;

import me.gacl.domain.User;

import org.apache.ibatis.annotations.Delete;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.Update;

/*

 *定义sql语句映射的接口,使用注解指明方法要执行的SQL的语句

 *

 */

public interface UserMapperInterface {
//使用@Insert注解指明add方法要执行的SQL语句
@Insert("insert into users(name,age) values(#{name},#{age})")

public int add(User user);

 //使用@Delete注解指明deleteById方法要执行的SQL
@Delete("delete from users where id = #{id}")
public int deleteById(int id);

 //使用@Update注解指明update方法要执行的SQL
@Update("update users set name=#{name},age=#{age} where id=#{id}")

public int update(User user);

//使用@Select注解指明getById方法要执行的SQL
@Select("select * from  users where id = #{id}")

public User getById(int id);

 //使用@Select注解指明getAll方法要执行的SQL
@Select("select * from users")
public List<User> getAll();

}

注意:我们不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。

核心代码: UserMapperInterface mapper = sqlSession.getMapper(UserMapperI.class);//myBatis根据参数:接口的字节码文件,动态生成实现该接口的实现类

conf.xml配置文件中搭建环境的<environments>下面的<mappers>标签中注册映射接口  <mapper class="me.gacl.mapping.UserMapperI"/>,团队协作把class改为resource=某配置文件

<?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>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="3333" />
</dataSource>
</environment>
</environments>

<mappers>
         <!-- 注册userMapper.xml文件, 
         userMapper.xml位于mapping这个包下,所以resource写成mapping/userMapper.xml-->
        <mapper resource="me/gacl/mapping/userMapper.xml"/>
        <mapper class="mapping.UserMapperInterface"/>
     </mappers>
     

</configuration>
封装了提供了服务service代码





package me.gacl.service;

import java.util.List;

import me.gacl.domain.User;
import me.gacl.mapping.UserMapperI;
import me.gacl.util.MyBatisUtil;

import org.apache.ibatis.session.SqlSession;

public class serviceByAnnotation {
//首先通过工具包拿到SqlSession,就是通过读取配置文件生成工厂拿到这个SqlSession的对象,它才是跟数据库打交道的对象
private SqlSession sqlSession = MyBatisUtil.getSqlSession(true);

//核心语句:这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可。它直接实现了接口中所有的方法,方法执行的SQL语句已经写在了注解中
private UserMapperInterface userMapper = sqlSession.getMapper(UserMapperI.class);
//也就是说,现在的userMapper已经是一个接口的实现类了
public int  addUser(User user){
return userMapper.add(user);
}
public int deleteById(int id){
return userMapper.deleteById(id);
}
public int update(User user){
return userMapper.update(user);
}
public User selete(int id){
return userMapper.getById(id);
}
public List<User> getAll(){
return userMapper.getAll();
}
//主函数用于测试,懒得创建测试包
public static void main(String[] args) {
serviceByAnnotation service = new serviceByAnnotation();//创建服务对象

// User user = new User("学霸大宇",21);

// int i =service.addUser(user);

//// System.out.println(i);

// int i = service.update(new User(8,"注释",170608));

// System.out.println(i);

// User user = service.selete(1);

// System.out.println(user);
List<User> list = service.getAll();
for(User user : list){
System.out.println(user);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐