依賴:
dependencies>
<!--mybatis依賴-->
<dependency>java
<groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version>
</dependency>
<!--數據庫驅動包-->
<dependency>mysql
<groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version>
</dependency>
<!--log4j日誌包(查看控制檯中mybatis生成的sql語句)-->
<dependency>sql
<groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version>
</dependency>
<dependency>數據庫
<groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version>
</dependency>
<dependency>apache
<groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version>
</dependency>
</dependencies>
實體類:
public class User { private int id;mybatis
private String username; private Date birthday; private String sex; private String address;//省略getter/setter
}
config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurationapp
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>ide
<properties resource="jdbc.properties"></properties> <settings> <setting name="lazyLoadingEnabled" value="true"/> </settings> <typeAliases> <package name="test.mylo.entity"></package> </typeAliases> <!--environments配置數據庫環境(事務、鏈接池、鏈接參數)--> <!--default 表示默認使用哪一個環境--> <environments default="mysql"> <environment id="mysql"> <!--指定事務管理器--> <transactionManager type="JDBC"></transactionManager> <!--指定數據源(鏈接池)--> <dataSource type="POOLED"> <property name="driver" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </dataSource> </environment> </environments> <mappers> <package name="test.mylo.mapper"></package> </mappers>
</configuration>
UserMapper
package test.mylo.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import test.mylo.entity.User;
public interface UserMapper {測試
@Select("select * from user where id = #{id}") User findOne(Integer id); @Insert("insert into user(username,birthday,sex,address) values(" + "#{username},#{birthday},#{sex},#{address})") @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id") void addUser(User user); @UpdateProvider(method = "testUpdate" ,type = TestSQL.class) User updateUser(User u);
}
TestSQL
package test.mylo.mapper;import test.mylo.entity.User;ui
import org.apache.ibatis.jdbc.SQL;
/**
**/
public class TestSQL {
public String testUpdate(final User user){ return new SQL(){{ UPDATE("user"); if (user.getUsername() != null){ SET(" username = #{username}"); } if(user.getBirthday() != null){ SET("birthday =#{birthday}"); } if(user.getAddress() != null){ SET("address = #{address}"); } WHERE("id = #{id}"); } }.toString(); }
}
Test
@Testpublic void testUpdateUser(){
SqlSession sqlSession = build.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setId(50); user.setUsername("test33"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("test333"); mapper.updateUser(user); sqlSession.commit();
}