一、原生MyBatis执行
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.SQLException;
public class TestMySql {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public void mySql() throws SQLException {
String sql = "select * from User";
SqlSession sqlSession = openSession();
sqlSession.getConnection().prepareStatement(sql);
closeSession(sqlSession);
}
private SqlSession openSession() {
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
return sqlSessionFactory.openSession();
}
private void closeSession(SqlSession sqlSession) {
sqlSession.close();
}
}
二、MyBatis 执行
2.1、调用dao
String sql = "Select * From User";
sqlMapper.explainQuery(sql);
2.2、dao层接口配置
@SqlParser(filter = true)
void explainQuery(String sql);
2.3、dao中xml 配置
<update id="explainQuery">
${templateName,jdbcType=VARCHAR}
</update>
三、MyBatis-plus中Sql注入器
3.1、编写MyBaseMapper(要添加方法)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
public interface MyBaseMapper<T> extends BaseMapper<T> {
List<T> findAll();
}
3.2、编写FindAll(方法具体实现)
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
public class FindAll extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sqlMethod = "findAll";
String sql = "select * from " + tableInfo.getTableName();
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatement(mapperClass, sqlMethod, sqlSource, modelClass, tableInfo);
}
}
3.3、编写MySqlInjector(注册到MyBatis-plus中)
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
List<AbstractMethod> methodList = super.getMethodList();
methodList.add(new FindAll());
list.add(new FindAll());
return methodList;
}
}
3.4、编写MySqlInjector(注册到MyBatis-plus中)
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import java.util.List;
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
List<AbstractMethod> methodList = super.getMethodList();
methodList.add(new FindAll());
list.add(new FindAll());
return methodList;
}
}
### 2.4、注册到Spring容器
```java
@Bean
public MySqlInjector mySqlInjector() {
return new MySqlInjector();
}
3.5、测试
@Test
public void testFindAll() {
List<User> users = this.userMapper.findAll();
for (User user : users) {
System.out.println(user);
}
}
|