maven数据库的增删改查

mapper


  <sql id="Base_Column_List" >
    id, username, password, model,miles,year,purchase,maintain
  </sql>

  <--! 查-->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  
  <--! 查看所有的-->
  <select id="selectAll" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
  </select>
  
  
  <--! 删除-->
  <delete id="deleteByPrimaryKey"  parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  
  <--! 增加-->
  <insert id="insert" parameterType="com.javen.model.User" >
    insert into user (id, username, password, model,miles,year,purchase,maintain)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{model,jdbcType=VARCHAR},#{miles,jdbcType=VARCHAR},#{year,jdbcType=VARCHAR},#{purchase,jdbcType=VARCHAR},#{maintain,jdbcType=VARCHAR})
  </insert>
  
  
  <--! 更改-->
  <update id="updateByPrimaryKey"  parameterType="com.javen.model.User" >
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      model = #{model,jdbcType=VARCHAR},
      miles = #{miles,jdbcType=VARCHAR},
      year = #{year,jdbcType=VARCHAR},
      purchase = #{purchase,jdbcType=VARCHAR},
      maintain = #{maintain,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
</mapper>

dao层

import com.javen.model.User;
//dao层接口增删改查
public interface IUserDao {
	
	int insert(User user); 
	
	int deleteByPrimaryKey(int id);
	
	int updateByPrimaryKey(User user);
	
	User selectByPrimaryKey(int id);
	
	List<User> selectAll();
	
}

service层

public interface IUserService {  
	
   public int insert(User user);
   
   public int deleteByPrimaryKey(int id);
   
   public int updateByPrimaryKey(User user);
	
   public User selectByPrimaryKey(int id);
   
   public List<User> selectAll();
  
}  

servicelmpl

//实现service中的方法

@Service("userService")  
public class UserServiceImpl implements IUserService {

	@Resource
	private IUserDao userDao;
	
	
	public int insert(User user) {
		return this.userDao.insert(user);
	}


	public int deleteByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		return this.userDao.deleteByPrimaryKey(id);
	}


	public int updateByPrimaryKey(User user) {
		// TODO Auto-generated method stub
		return this.userDao.updateByPrimaryKey(user);
	}


	public User selectByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		return this.userDao.selectByPrimaryKey(id);
	}


	public List<User> selectAll() {
		// TODO Auto-generated method stub
		return this.userDao.selectAll();
	}  
	
}  

controller层

 @RequestMapping(value="/insert", method=RequestMethod.GET)  
    public String insert(HttpServletRequest request,Model model){  
    	String id = request.getParameter("id");
    	String password = request.getParameter("password");
    	
    	User user = new User();
    	user.setId(Integer.valueOf(id));
    	user.setPassword(password);
    	//System.out.println(user.toString());
		userService.insert(user);
        return "index"; 
    }  
    @RequestMapping(value="/deleteById", method=RequestMethod.GET)  
    public String deleteById(HttpServletRequest request,Model model){  
    
    	Integer id = Integer.valueOf(request.getParameter("id"));
    	
    	userService.deleteByPrimaryKey(id);
    	
		
        return "index"; 
    }  
    
    @RequestMapping(value="/updateByPrimaryKey", method=RequestMethod.GET)  
    public String updateByPrimaryKey(HttpServletRequest request,Model model){  
    
    	String model1 = request.getParameter("model");
    	String miles = request.getParameter("miles");
    	String year = request.getParameter("year");
    	String purchase = request.getParameter("purchase");
    	String maintain = request.getParameter("maintain");
    	
    	User user = new User();
    	user.setModel(model1);
    	user.setMiles(miles);
    	user.setYear(year);
		user.setPurchase(purchase);
		user.setMaintain(maintain);
		userService.updateByPrimaryKey(user);
		
        return "index"; 
    } 
    @RequestMapping(value="/selectByPrimaryKey", method=RequestMethod.GET)  
    public String selectByPrimaryKey(HttpServletRequest request,Model model){  
    
    	Integer id = Integer.valueOf(request.getParameter("id"));
    	
    	userService.selectByPrimaryKey(id);
    	
		
        return "index"; 
    }  
     
    @RequestMapping(value="/selectAll", method=RequestMethod.GET)  
    public JSONObject selectAll(HttpServletRequest request,Model model){  
    
    	List<User> data= new ArrayList<User>();
    	data = userService.selectAll();
		//System.out.println(data);
        return "User";
    	
    	
    }  
    
}  


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: JavaWeb连接MySQL数据库实现增删改查的步骤如下: 1. 导入MySQL驱动包 在项目导入MySQL驱动包,可以使用Maven或手动导入。 2. 创建数据库连接 使用JDBC API创建数据库连接,需要指定数据库的URL、用户名和密码。 3. 创建Statement对象 使用连接对象创建Statement对象,用于执行SQL语句。 4. 执行SQL语句 使用Statement对象执行SQL语句,可以是增、删、改或查操作。 5. 处理结果集 如果执行的是查询操作,需要使用ResultSet对象处理查询结果。 6. 关闭连接 使用完数据库连接后,需要关闭连接对象、Statement对象和ResultSet对象。 以上就是JavaWeb连接MySQL数据库实现增删改查的基本步骤。 ### 回答2: 在JavaWeb项目使用MySQL数据库进行增删改查的操作,需要先进行配置连接数据库的信息。以Eclipse开发工具为例,首先需要在项目的lib目录下导入MySQL驱动包,接着在项目的web.xml配置数据库连接的信息,包括数据库驱动名、URL、用户名和密码等。在数据库连接成功后,就可以进行增删改查的操作了。 增加数据:使用JDBC执行INSERT语句插入一条记录,需要构造SQL语句、设置参数并执行更新操作。 删除数据:同样是使用JDBC执行DELETE语句删除一条记录,需要构造SQL语句、设置参数并执行更新操作。 修改数据:使用JDBC执行UPDATE语句更新一条记录,需要构造SQL语句、设置参数并执行更新操作。 查询数据:使用JDBC执行SELECT语句查询记录,需要构造SQL语句、设置参数并执行查询操作。查询结果可以通过ResultSet对象进行遍历,获取数据后可以进行进一步的处理。 在JavaWeb项目,可以通过Servlet和JSP来实现数据库增删改查操作。在Servlet,可以将请求参数封装为JavaBean对象,通过DAO层操作数据库并返回处理后的结果。在JSP,可以通过EL表达式和JSTL标签进行数据的展示和处理。为了保证安全性和可维护性,需要使用预处理语句、关闭数据库连接、使用连接池等技术。 ### 回答3: Java Web开发,连接MySQL数据库进行增删改查操作是一项非常常见的任务。MySQL数据库是一个开源的数据库系统,提供良好的稳定性和高性能的支持,加上Java语言优秀的兼容性,两者配合使用可以实现非常优秀的数据库操作。 首先,需要在Java Web项目的classpath路径下引入mysql驱动jar包。具体操作步骤可以参考这篇文章:https://www.runoob.com/java/java-mysql-connect.html 然后,使用Java代码连接MySQL数据库,可以采用JDBC或者DAO层框架。 使用JDBC的话,可以按照以下步骤进行: 1. 加载MySQL驱动程序:Class.forName("com.mysql.jdbc.Driver"); 2. 创建数据库连接:Connection conn = DriverManager.getConnection(url, username, password); 3. 创建执行SQL语句的Statement对象:Statement stmt = conn.createStatement(); 4. 执行SQL语句并获取结果:ResultSet rs = stmt.executeQuery(sql); 5. 处理结果集并关闭连接:while(rs.next()){...}rs.close();stmt.close();conn.close(); 通过JDBC连接MySQL数据库进行增删改查操作,只需要构造相应SQL语句,调用JDBC API即可。例如: //增加 String sql = "INSERT INTO user(name,password) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "Tom"); pstmt.setString(2, "123"); int num = pstmt.executeUpdate(); //删除 String sql = "DELETE FROM user WHERE id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); int num = pstmt.executeUpdate(); //修改 String sql = "UPDATE user SET password=? WHERE id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "456"); pstmt.setInt(2, 2); int num = pstmt.executeUpdate(); //查询 String sql = "SELECT * FROM user"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String pwd = rs.getString("password"); } 另外,使用DAO层框架可以进一步简化连接MySQL数据库的操作。常见的DAO层框架有Hibernate、MyBatis等,这些框架可以大大简化增删改查操作,提高开发效率和代码复用性。 总而言之,使用Java Web连接MySQL数据库进行增删改查操作并不困难,只需要正确引入驱动程序、构造SQL语句、选择适合自己的连接方式即可。当然,良好的编程习惯和性能优化也是不可或缺的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值