MyBatis语句构造器
- 写代码中最痛苦的事情之一就是在 Java 代码中嵌入 SQL 语句(SQL语句很熟练请忽略)。这通常是因为需要动态生成 SQL 语句,不然我们可以将它们放到外部文件或者存储过程中。 MyBatis 在 XML 映射中具备强大的 SQL 动态生成能力。但有时,我们还是需要在 Java 代码里构建 SQL 语句。此时,MyBatis 有另外一个特性可以帮到你,让你从处理典型问题中解放出来,比如加号、引号、换行、格式化问题、嵌入条件的逗号管理及 AND 连接。语句构造器可以减少这些问题
- MyBatis配置文件
<?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>
<!--引入外部配置文件db.properties-->
<properties resource="db.properties" />
<!--起别名使用package标签可以匹配com.mybatis.pojo包下的所有类,并且不区分大小写
也可使用typeAlias标签匹配指定类型-->
<typeAliases>
<package name="com.yanm.pojo" />
</typeAliases>
<!--配置mybatis环境变量-->
<environments default="development">
<environment id="development">
<!--配置JDBC事务控制,由mybatis进行管理-->
<transactionManager type="JDBC"/>
<!--配置数据源,采用mybatis连接池-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pass}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<package name="com.yanm.dao"/>
</mappers>
</configuration>
driver=org.mariadb.jdbc.Driver
url=jdbc:mariadb://localhost:3306/hello
user=root
pass=root
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int uid;
private String username;
private String password;
private int age;
private String addr;
}
- dao层:IUserDaoProvider.java接口
public interface IUserDaoProvider {
//查询用户信息
@SelectProvider(type = SQLProvider.class,method = "getAllUser")
List<User> getAllUsers();
@SelectProvider(type = SQLProvider.class,method = "getUser")
List<User> getUsers();
@InsertProvider(type =SQLProvider.class,method = "saveUser")
int saveUser();
@InsertProvider(type =SQLProvider.class,method = "saveUserOne")
int saveUserOne(User user);
}
- dao层:通过构造器实现SQL语句SQLProvider.java
public class SQLProvider {
public String getAllUser(){
return new SQL()
.SELECT("uid","username","password")
.SELECT("age","addr")
.FROM("user")
.toString();
}
public String getUser(){
return new SQL()
.SELECT("uid","username","password")
.SELECT("age","addr")
.FROM("user")
.WHERE("uid=1")
.OR()
.WHERE(("username='小乔'"))
.toString();
}
public String saveUser(){
return new SQL()
.INSERT_INTO("user")
.VALUES("username","'yanmeng'")
.VALUES("password","'yanmeng'")
.VALUES("age","'18'")
.VALUES("addr","'yanmeng'")
.toString();
}
public String saveUserOne(User user){
return new SQL()
.INSERT_INTO("user")
.VALUES("username","#{username}")
.VALUES("password","#{password}")
.VALUES("age","#{age}")
.VALUES("addr","#{addr}")
.toString();
}
}
public class TestSQL {
private SqlSession ss=null;
@Before
public void beforeSQL(){
ss= SessionUtils.getSqlSession();
}
@After
public void afterSQL(){
SessionUtils.SqlSessionClose(ss);
}
@Test
public void getAllUser(){
IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);
List<User> users = dao.getAllUsers();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void getUser(){
IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);
List<User> users = dao.getUsers();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void saveUser(){
IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);
int i = dao.saveUser();
System.out.println(i);
}
@Test
public void saveUserOne(){
IUserDaoProvider dao=ss.getMapper(IUserDaoProvider.class);
User user=new User();
user.setUsername("mengy");
user.setPassword("mengy");
user.setAge(49);
user.setAddr("anhui");
int i = dao.saveUserOne(user);
System.out.println(i);
}
}
public class SessionUtils {
private static SqlSessionFactory ssf=null;
private static SqlSession ss=null;
static {
try {
ssf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession对象
public static SqlSession getSqlSession(){
ss=ssf.openSession(true);
return ss;
}
//关闭SqlSession对象
public static void SqlSessionClose(SqlSession s){
if (s!=null){
s.close();
s=null;
}
}
}
网友评论