mybatis SqlSession session = sqlSessionFactory.openSession(); 报空指针异常 110
@Controller@RequestMapping("/article")publicclassUserController{@AutowiredIUserOperat...
@Controller
@RequestMapping("/article")
public class UserController {
@Autowired
IUserOperation userMapper;
SqlSessionFactory sqlSessionFactory;
Reader reader;
{
try{
reader = Resources.getResourceAsReader("Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
}
}
public SqlSessionFactory getSession(){
return sqlSessionFactory;
}
@RequestMapping("/main.do")
public ModelAndView main(HttpServletRequest request,HttpServletResponse response){
ModelAndView mav=new ModelAndView("main");
return mav;
}
@RequestMapping("/adduser.do")
public void add(HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("name");
String age = request.getParameter("age");
String address = request.getParameter("address");
User user = new User();
user.setUserName(name);
user.setUserAge(age);
user.setUserAddress(address);
SqlSession session = sqlSessionFactory.openSession(); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
try {
userMapper.addUser(user);
session.commit();
} finally {
session.close();
}
}
}
代码如下。!!!!!!!!!!!!!那行报空指针。求高手解答 展开
@RequestMapping("/article")
public class UserController {
@Autowired
IUserOperation userMapper;
SqlSessionFactory sqlSessionFactory;
Reader reader;
{
try{
reader = Resources.getResourceAsReader("Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
}
}
public SqlSessionFactory getSession(){
return sqlSessionFactory;
}
@RequestMapping("/main.do")
public ModelAndView main(HttpServletRequest request,HttpServletResponse response){
ModelAndView mav=new ModelAndView("main");
return mav;
}
@RequestMapping("/adduser.do")
public void add(HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("name");
String age = request.getParameter("age");
String address = request.getParameter("address");
User user = new User();
user.setUserName(name);
user.setUserAge(age);
user.setUserAddress(address);
SqlSession session = sqlSessionFactory.openSession(); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
try {
userMapper.addUser(user);
session.commit();
} finally {
session.close();
}
}
}
代码如下。!!!!!!!!!!!!!那行报空指针。求高手解答 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |