myBatis example【转】

http://guagreat.iteye.com/blog/2324900

mybatis的通用mapper的通用Example使用,当然也有不对应通用mapper的普通Example使用

想要偷懒不写mybatis里的一大堆mapper.xml文件,就想使用通用的Mapper类减少工作量。

    首先,我使用的是Maven项目,所以导入Mapper的Maven依赖

 

[html]  view plain  copy
 
  1. <dependency>  
  2.   <groupId>tk.mybatis</groupId>  
  3.   <artifactId>mapper</artifactId>  
  4.   <version>3.2.0</version>  
  5. </dependency>    

    同时有一项必要依赖项:项目依赖于JPA的注解,需要添加Maven依赖:

[html]  view plain  copy
 
  1. <dependency>  
  2.   <groupId>javax.persistence</groupId>  
  3.   <artifactId>persistence-api</artifactId>  
  4.   <version>1.0</version>  
  5. </dependency>  

    接下来,在配置文件applicationContext.xml中配置Mapper

[html]  view plain  copy
 
  1. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">  
  2.     <property name="basePackage" value="com.isscas.ucqcs.common.dao"/>  
  3.     <property name="properties">  
  4.         <value>  
  5.             mappers=tk.mybatis.mapper.common.Mapper      //这是Mapper接口配置,当接口为此默认配置时,可不写  
  6.         </value>  
  7.     </property>  
  8. </bean>  

    直接将MyBatis的配置 org 修改为 tk 即可

[html]  view plain  copy
 
  1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

    到这里,Mapper的配置已经全部完成。

 

    只要在自己的Mapper接口上继承Mapper<T>接口,即可调用通用Mapper类中全部的方法。

    另外要注意的是:该Mapper类对实体类有自己的解析方式  :  表名和字段名会默认使用类名,驼峰转下划线(即UserNamed对应表名/字段名user_name),使用@Column(name = "真实名称")可以指定表名/字段名。

    另,需要@Id标记主键字段,对不需要的字段,可用@Tranisent忽略

    Mapper接口中包含单表的增删改查分页功能。

    下面给出一个查询实例:

[html]  view plain  copy
 
  1. CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);  
  2. //查询全部  
  3. List<Country> countryList = mapper.select(new Country());  
  4. //总数  
  5.   
  6. //通用Example查询  
  7. Example example = new Example(Country.class);  
  8. example.createCriteria().andGreaterThan("id", 100);//这里给出的条件查询为id>100  
  9. countryList = mapper.selectByExample(example);  

项目示例:

@Override

public String getSimpleOfTree(String customerKey) {

// TODO Auto-generated method stub

Example example = new Example(TbCusFirmrelgroup.class);///注意用的是类中的属性,不是数据库中的属性

example.setOrderByClause("groupKey asc");//注意用的是类中的属性,不是数据库中的属性

example.createCriteria().andEqualTo("customerKey",BigDecimal.valueOf(Long.valueOf(customerKey)));

List<TbCusFirmrelgroup> cusPowerGroupList =  tbCusFirmrelgroupMapper.selectByExample(example);

return null;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's a simple example of using MyBatis: First, you need to set up your database connection and create a configuration file for MyBatis. In this example, let's assume you have a table called "users" with columns "id" and "name". 1. Create a configuration file named "mybatis-config.xml" with the following content: ```xml <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/mydatabase"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration> ``` 2. Create a Java class called "User" representing the users table: ```java public class User { private int id; private String name; // Getters and setters } ``` 3. Create a mapper XML file named "UserMapper.xml" with the following content: ```xml <mapper namespace="com.example.UserMapper"> <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> </resultMap> <select id="getUserById" resultMap="userResultMap"> SELECT id, name FROM users WHERE id = #{id} </select> <insert id="insertUser" parameterType="User"> INSERT INTO users (name) VALUES (#{name}) </insert> </mapper> ``` 4. Create an interface called "UserMapper" with the following content: ```java public interface UserMapper { User getUserById(int id); void insertUser(User user); } ``` 5. Finally, you can use MyBatis in your Java code: ```java public class Main { public static void main(String[] args) { // Create a SqlSessionFactory InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // Create a SqlSession try (SqlSession session = sqlSessionFactory.openSession()) { // Get the UserMapper instance UserMapper userMapper = session.getMapper(UserMapper.class); // Get a user by ID User user = userMapper.getUserById(1); System.out.println(user.getName()); //
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值