将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。所以该整合,只需要将 SqlSessionFactory 的对象生成器SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。
package com.ambow.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int age;
}
4、定义 StudentDao 接口
代码如下:
package com.ambow.dao;
import com.ambow.pojo.Student;
import java.util.List;
public interface StudentDao {
int insertStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int id);
Student selectStudentById(int id);
List selectAllStudents();
}
5、定义映射文件 mapper
在 Dao 接口的包中创建 MyBatis 的映射文件 mapper,命名与接口名相同,本例为 StudentDao.xml
mapper 中的 namespace 取值也为 Dao 接口 的全限定性名。
代码如下:
insert into student2 values(null,#{name},#{age})
update student2 set name = #{name},age = #{age} where id = #{id}
delete from student2 where id = #{id}
6、定义 Service 接口和实现类
接口定义:
代码如下:
package com.ambow.service;
import com.ambow.pojo.Student;
import java.util.List;
public interface StudentService {
int addStudent(Student student);
int modifyStudent(Student student);
int removeStudent(int id);
Student findStudentById(int id);
List findAllStudents();
}
实现类定义:
代码如下:
package com.ambow.service.impl;
import com.ambow.dao.StudentDao;
import com.ambow.pojo.Student;
import com.ambow.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
//引入StudentDao的对象
@Autowired
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
return studentDao.insertStudent(student);
}
@Override
public int modifyStudent(Student student) {
return studentDao.updateStudent(student);
}
@Override
public int removeStudent(int id) {
return studentDao.deleteStudent(id);
}
@Override
public Student findStudentById(int id) {
return studentDao.selectStudentById(id);
}
@Override
public List findAllStudents() {
return studentDao.selectAllStudents();
}
}
7、定义 MyBatis 主配置文件
在 src 下定义 MyBatis 的主配置文件,命名为 mybatis.xml
这里有两点需要注意:
(1)主配置文件中不再需要数据源的配置了。因为数据源要交给 Spring 容器 来管理了。
(2)这里对 mapper 映射文件的注册,使用标签,即只需给出 mapper 映射文件所在的包即可。因为 mapper 的名称与 Dao 接口名相同, 可以使用这种简单注册方式。
这种方式的好处是:若有多个映射文件,这里的 配置也是不用改变的。当然,也可使用原来的标签方式。
8 、修改 Spring 配置文件
(1) 数据源的配置(掌握)
使用 JDBC 模板,首先需要配置好数据源,数据源直接以 Bean 的形式配置 在 Spring 配置文件中。根据数据源的不同,其配置方式不同:
如果希望workflow存储最近20次的log,在session里的Config Object设置,log options做配置,save session log :sessions run ;savesessio log for these runs:20
session下面的source 里面有个tracing 
今天遇到一个客户BUG,当前的jdbc连接用户是root,然后部分删除操作都会报下面这个错误:The user specified as a definer ('aaa'@'localhost') does not exist
最后找原因发现删除操作做了触发器,而触发器里面有这样一句
/*!50017 DEFINER = ''aaa@'localhost' */
原来最初
O7_DICTIONARY_ACCESSIBILITY参数控制对数据字典的访问.设置为true,如果用户被授予了如select any table等any table权限,用户即使不是dba或sysdba用户也可以访问数据字典.在9i及以上版本默认为false,8i及以前版本默认为true.如果设置为true就可能会带来安全上的一些问题.这也就为什么O7_DICTIONARY_ACCESSIBIL
#h1#
0、完成课堂例子
1、将一个四位数逆序打印
1234 ==> 4321
实现方法一:
# include <stdio.h>
int main(void)
{
int i = 1234;
int one = i%10;
int two = i / 10 % 10;
int three = i / 100 % 10;
===================================================================
第一个
===================================================================
try{
CString sql;
sql.Format("select * from p