一个小的Dao层、service层、controller层代码示例

//Dao层

package com.bear.cakeonline.test;



import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;


import com.bear.cakeonline.entity.UserInfo;
import com.bear.cakeonline.entity.UserLogin;


@Repository
public class TestDaoImpl {


@Resource
private SessionFactory sessionFactory;

public void saveUser(){
UserLogin ul=new UserLogin();
ul.setLoginName("zs");
ul.setPassword("123");

UserInfo ui=new UserInfo();
ui.setRealName("zhangsan");
ul.setUserInfo(ui);
ui.setUserLogin(ul);
Session s=this.sessionFactory.openSession();
Transaction t=s.beginTransaction();
s.save(ul);
t.commit();
s.close();
}

}


//service层

package com.bear.cakeonline.test;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(readOnly=false)
public class TestServiceImpl {


@Resource
private TestDaoImpl testDaoImpl;

public void regist(){
this.testDaoImpl.saveUser();
}
}



//controller层

package com.bear.cakeonline.test;


import javax.annotation.Resource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/test")
public class TestController {

@Resource
private TestServiceImpl testServiceImpl;


@RequestMapping("/regist")
public String regist(){
System.out.println("lskdfj");
this.testServiceImpl.regist();
return "";
}
}

在典型的Java Web应用程序中,Controller负责接收并处理HTTP请求,Service负责业务逻辑的处理,DAO负责与数据库的交互。Mapper是MyBatis框架中的一,用于映射数据库表与Java对象之间的关系。 通常情况下,Controller会调用Service的方法来处理业务逻辑,Service会调用DAO的方法来操作数据库。Mapper则是通过MyBatis框架自动调用与数据库交互的方法。 下面是一个简单的示例代码,演示了Controller如何调用ServiceService如何调用DAO,以及如何使用Mapper进行数据库操作。 Controller代码: ``` @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ``` Service代码: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectByPrimaryKey(id); } } ``` DAO代码: ``` @Repository public interface UserMapper { int deleteByPrimaryKey(Long id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); } ``` Mapper代码: ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="userMap" type="com.example.entity.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="selectByPrimaryKey" resultMap="userMap" parameterType="java.lang.Long"> SELECT id, username, password FROM user WHERE id = #{id,jdbcType=BIGINT} </select> </mapper> ``` 在这个示例中,Controller通过调用UserService的getUserById方法来获取一个用户对象。Service则调用UserMapper的selectByPrimaryKey方法从数据库中获取用户数据。Mapper则是通过配置XML文件来实现与数据库的交互。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值