上篇是使用xml配置来完成mysql数据库的操作,本文将使用注解形式,当然由于本文是较基础的入门级,存在许多地方欠考虑的地方,请大家多包涵。由于代码较简单就省略注释和讲解了。
直接使用类来生成Mapper:
package com.nothing.Mapper;
import com.nothing.Model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from user_nothing where id = #{id}")
User findUserById(int id);
@Insert("insert into user_nothing(username,password) values(#{username},#{password})")
void addUser(User user);
@Select("select * from user_nothing")
List<User> getAllUsers();
}
三个方法分别是向mysql数据库中user_nothing表单条查询、添加、列表查询。
基本上与userMapper.xml的句子一直(这里添加了一个查询所有用户的语句),这里再次黏贴一下代码,便于比较:
<mapper namespace="userMapper">
<!--根据ID获取对应的值 -->
<select id="findUserById" parameterType="int" resultType="com.nothing.Model.User">
select * from user_nothing where id = #{id}
</select>
<insert id="addUser" parameterType="com.nothing.Model.User">
insert into user_nothing(username,password) values(#{username},#{password})
</insert>
</mapper>
本文因此就不需要userMapper.xml了。
需要修改的config没什么改动,仅仅需要注意添加的mapper即可。
<?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>
<!-- development:开发模式 work:工作模式 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mysql" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.nothing.Mapper.UserMapper"/>
</mappers>
</configuration>
为便于读者使用将之前的Dao和Service类都再贴出来:
public interface IUserDao {
User findUserById(int id);
void addUser(User user);
List<User> getAllUsers();
}
注意注释部分是使用xml时配置。
public class UserDaoImpl implements IUserDao{
private SqlSessionFactory sessionFactory;
private SqlSession session;
private UserMapper mapper;
public UserDaoImpl() {
String resource = "conf.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sessionFactory.openSession();
mapper = session.getMapper(UserMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
public User findUserById(int id) {
// String statement = "userMapper.findUserById";
// User user = (User)session.selectOne(statement, id);
// return user;
return mapper.findUserById(id);
}
public void addUser(User user) {
// String statement = "userMapper.addUser";
// session.insert(statement, user);
mapper.addUser(user);
session.commit();
}
public List<User> getAllUsers() {
return mapper.getAllUsers();
}
}
public interface IUserService {
User findUserById(int id);
void addUser(User user);
List<User> getAllUsers();
}
public class UserServiceImpl implements IUserService{
private IUserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
}
public User findUserById(int id) {
return userDao.findUserById(id);
}
public void addUser(User user){
userDao.addUser(user);
}
public List<User> getAllUsers() {
return userDao.getAllUsers();
}
}
这里唠叨一下,最近学习mvp模式时,突然觉得移动的mvp框架不就是这个Service作用么。
最后贴一下Controller
@Controller
public class MainController {
private IUserService service = new UserServiceImpl();
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index( User user) {
return "index";
}
@RequestMapping(value="nice",method = RequestMethod.GET)
@ResponseBody
public List<User> nice(Model model){
return service.getAllUsers();
}
@RequestMapping(value ="/toJson",method=RequestMethod.POST)
@ResponseBody
public User toJson(User user){
service.addUser(user);
return service.findUserById(2);
}
}
运行返回所有用户的页面截图的结果图。是不是很简单?