`
NeverFlyAway
  • 浏览: 69799 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

Mybatis Mapper的class写法

阅读更多

使用Mybatis的时候需要写mapper.xml来映射实体类和数据表字段

mapper.xml也是官方推荐的用法,现在我们用带注解的class来实现mapper.xml

 

代码如下:

顺便配置了一下CRUD的缓存,如果不用缓存,把@CacheNamespace和@Options去掉就行,他们配置的是缓存类型和缓存时间

public class User {

	private String userName;
	private String password;
	private Boolean enable;

	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 Boolean getEnable() {
		return enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

}

 

 

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@CacheNamespace
public interface UserMapper {
	
	@Options(flushCache = true, timeout = 20000)
	@Insert("insert into demo.user(user_name, password, enable) values(#{userName}, #{password}, #{enable})")
	public void insert(User object);

	@Options(flushCache = true, timeout = 20000)
	@Update("update demo.user set password=#{password}, enable=#{enable} where user_name=#{userName}")
	public void update(User object);

    @Options(useCache = true, flushCache = false, timeout = 10000)
    @Select("select * from demo.user where user_name=#{userName}")
	@Results(value = {
			@Result(id = true, property = "userName", column = "user_name"),
			@Result(property = "password", column = "password"),
			@Result(property = "enable", column = "enable"),
		})
	public List<User> query(@Param("userName") String userName);

    @Options(flushCache = true, timeout = 20000)
    @Delete("delete from demo.user where user_name=#{userName}")
	public void delete(@Param("userName") String userName);

}

 

使用:

@Autowired
private SqlSessionFactory sqlSessionFactory;

@Test
public void testInsert() {
    User user = new User("username", "password", true);
    sqlSessionFactory.openSession().getMapper(UserMapper.class).insert(user);
}

 

注意,如果需要映射一对多的关系,还是老老实实用xml,毕竟mybatis的注解还不完善,简单的实体用class风格还是不错的,挺方便,而且如果需要mapper.xml和class风格的mapper混用,也是可以滴(在class中使用@ResultMap和mapper.xml进行合体)

 

以上

分享到:
评论

相关推荐

    Spring集成MyBatis简单demo

    &lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt; &lt;property name="basePackage" value="com.example.demo.mapper"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc....

    mybatis连接oracle实例

    如果你使用Spring框架,需要在Spring的配置文件中配置MyBatis的SqlSessionFactory和Mapper扫描器,使Spring能自动管理Mapper接口的实例。 7. **测试连接**: 在Eclipse中创建一个JUnit测试类,注入`TestService`...

    详解Mybatis通用Mapper介绍与使用

    通用Mapper介绍与使用 Mybatis通用Mapper是一种基于Mybatis的插件,旨在...2. 配置 Spring MVC:在 Spring MVC 配置文件中添加通用Mapper的配置:&lt;bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"&gt; ...

    mybatis-generator-1.3版本jar包及配置文件写法

    在本主题中,我们将详细探讨`mybatis-generator-1.3`版本的使用,包括配置文件的写法以及如何在Eclipse环境中集成并使用MBG插件来自动化生成Mapper文件和实体类。 首先,`mybatis-generator-1.3.jar`是MBG的主要...

    MyBatis通过 JDBC连接 PostgreSQL数据库 最小例子

    UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.selectUser(1); System.out.println(user); } catch (IOException e) { e.printStackTrace(); } ``` 以上就是使用MyBatis通过...

    mybatis中批量插入的两种方式(高效插入)

    FooMapper fooMapper = session.getMapper(FooMapper.class); int size = 10000; try { for (int i = 0; i ; i++) { Foo foo = new Foo(); foo.setName(String.valueOf(System.currentTimeMillis())); ...

    MyBatis 参数类型为String时常见问题及解决方法

    在使用MyBatis框架时,有时会遇到与String类型参数相关的问题,尤其是在编写Mapper XML文件时。本文将详细讨论这些问题以及相应的解决方案。 首先,我们来看第一个问题:**参数为String时的插值问题**。在MyBatis中...

    SSM使用mybatis分页插件pagehepler实现分页示例

    &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt; &lt;!-- 注意其他配置 --&gt; &lt;bean class="com.github.pagehelper.PageInterceptor"&gt; &lt;!-- 使用下面的方式配置参数,一行配置...

    spring boot多数据源配置

    public class MyBatisConfig { @Bean(name = "primarySqlSessionFactory") @ConfigurationProperties(prefix = "mybatis.primary") public SqlSessionFactory primarySqlSessionFactory(@Qualifier(...

Global site tag (gtag.js) - Google Analytics