会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132904个问题
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 30391楼
Python 全系列/第一阶段:Python入门/编程基本概念 30392楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 30395楼

from sqlalchemy import create_engine,Column,Integer,String,Float,DECIMAL,Boolean,Enum,Date,DateTime,Time,Text
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship
from datetime import date,datetime,time
from sqlalchemy import func,or_,ForeignKey
import enum
import random

#准备连接数据库信息
HOSTNAME="127.0.0.1"
PORT="3306"
DATABASE="first_sqlclchemy"
USERNAME="root"
PASSWORD="root"

#按照上述格式组织数据库信息
DB_URI="mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8".format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)

#创建数据库引擎
engine=create_engine(DB_URI)

#需求:创建好一个ORM模型 并映射到指定的数据库中成为  表
Base=declarative_base(engine)

session=sessionmaker(engine)()

#需求:sqlalchemy实现外键 以及四种约束
#主表/从表
class User(Base):
    __tablename__="user"
    id=Column(Integer,primary_key=True,autoincrement=True)
    uname=Column(String(50),nullable=False)
    #添加属性
    # newss=relationship("News")

    def __repr__(self):
        return "<User(uname:{0})>".format(self.uname)

class News(Base):
    __tablename__="news"
    id=Column(Integer,primary_key=True,autoincrement=True)
    title=Column(String(50),nullable=False)
    content=Column(Text,nullable=False)
    #实现外键 外键必须在从表中
    uid=Column(Integer,ForeignKey("user.id")) #默认删除策略为RESCRICT
    
    #添加属性 优化2表查询操作
    author=relationship("User",backref="newss")

    def __repr__(self):
        return "<News(title:{0},content:{1})>".format(self.title,self.content)

#创建表
# Base.metadata.drop_all()
# Base.metadata.create_all()

#添加测试数据
# user=User(uname="momo")
# session.add(user)
# session.commit()
#
# news1=News(title="AAA",content="123",uid=1)
# news2=News(title="BBB",content="456",uid=1)
# session.add_all([news1,news2])
# session.commit()

#需求:ORM层面外键和一对多关系
#查询某篇新闻的 作者
# news=session.query(News).first()
# print(news)
# print(news.uid) #拿到这篇新闻是哪个id用户写的
# user=session.query(User).get(news.uid)
# print(user)
# print(user.uname)

#总结:上述代码能实现需求,但是操作太麻烦,可以引入OMR模型进行优化

#进行优化
# news=session.query(News).first()
# print(news.author.uname)

#需求2 通过作者名字查询他写了哪些文章
users=session.query(User).first()
print(users.newss.title)
# news=session.query(News).first()
# print(news.author.uname)

老师 为什么这个查询就可以查到User类中的uname属性 

users=session.query(User).first()
print(users.newss.title)

这样为什么就不可以了 同样都是在类中添加一个属性 为什么这个就不能查询 直接报错

image.png

Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask数据库 30396楼
Python 全系列/第一阶段:Python入门/编程基本概念 30397楼

老师我的运行不出来

package FistServet01;
//下载本地文件
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DownloadFileServlet07 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //路径转换 ServletContext第一个作用相对路径转化为绝对路径
        String realPath = servletContext.getRealPath("image/cat.jpg");
        System.out.println(realPath);

        //获取文件
        File file = new File(realPath);
        FileInputStream fis = new FileInputStream(file);
        byte[] buff = new byte[fis.available()];
        fis.read(buff);
        //在响应中添加信息
        resp.addHeader("Content-Disposition","attachment;file+"+new String(file.getName().getBytes("gbk"),"iso-8859-1")); //实现文件下载要修改响应头添加附加信息
        OutputStream os = resp.getOutputStream();
        os.write(buff);
        os.flush();
        os.close();
    }
}

image.png

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 30399楼
Python 全系列/第四阶段:函数式编程和核心特性/内存管理(旧) 30400楼
JAVA 全系列/第三阶段:数据库编程/数据库范式与表关系 30402楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 30403楼

老师,在这节视频里面运行spring-boot:run命令的时候报错,错误信息如下:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.074 s
[INFO] Finished at: 2019-05-27T21:26:31+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.10.RELEASE:run (default-cli) on project 20-spring-boot-springloader: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.5.10.RELEASE:run failed: Plugin org.springframework.boot:spring-boot-maven-plugin:1.5.10.RELEASE or one of its dependencies could not be resolved: Failed to collect dependencies at org.springframework.boot:spring-boot-maven-plugin:jar:1.5.10.RELEASE -> org.apache.maven:maven-archiver:jar:2.6 -> org.apache.maven:maven-artifact:jar:3.1.1: Failed to read artifact descriptor for org.apache.maven:maven-artifact:jar:3.1.1: Could not transfer artifact org.apache.maven:maven-artifact:pom:3.1.1 from/to nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public): RSA premaster secret error: SunTls12RsaPremasterSecret KeyGenerator not available -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:

我在百度上搜了一些方式,但是发现都没啥用,希望老师解答一下,感谢,附源代码一份

20-spring-boot-springloader.zip


JAVA 全系列/(旧的隐藏)第十一阶段:spring全家桶(Spring Boot)/Spring Boot 30404楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637