IntelliJ IDEA spring mvc +mybatis 环境搭建服务器(下)

上篇是使用xml配置来完成mysql数据库的操作,本文将使用注解形式,当然由于本文是较基础的入门级,存在许多地方欠考虑的地方,请大家多包涵。由于代码较简单就省略注释和讲解了。

来自我的简书: http://www.jianshu.com/p/32f15e199572

直接使用类来生成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);
    }
}

运行返回所有用户的页面截图的结果图。是不是很简单?


Paste_Image.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值